使用时,多次检查null .Select()

时间:2016-05-27 10:44:17

标签: c#

我正在开展一个项目,在那里我反序列化XML,然后将其读取到数据库中。我能做到这一点就好了,只有一个问题。 问题是我想在反序列化之前能够在XML中检查两次null。现在我只能检查null一次。

我有以下代码:

Console.WriteLine("Deserilizing FolkbokföringsPost");

var myPersons = Deserialize<List<FolkbokforingspostTYPE>>()
    .Select(x => new Person
    {
        PersonalIdentityNumber = x.Personpost.PersonId.PersonNr != null ? x.Personpost.PersonId.PersonNr : null,
        SpecialIdentityNumber = x.Personpost.PersonId.Tilltalsnamnsmarkering != null ? x.Personpost.PersonId.Tilltalsnamnsmarkering : null,
        LastName = x.Personpost.Namn.Efternamn != null ? x.Personpost.Namn.Efternamn : null,
        FirstName = x.Personpost.Namn.Fornamn != null ? x.Personpost.Namn.Fornamn : null,
        NationalRegistrationCountyCode = x.Personpost.Folkbokforing.LanKod != null ? x.Personpost.Folkbokforing.LanKod : null,
        NationalRegistrationMunicipalityCode = x.Personpost.Folkbokforing.KommunKod != null ? x.Personpost.Folkbokforing.KommunKod : null,
        ForeignDistrubtionAddress1 = x.Personpost.Adresser.Utlandsadress != null ? x.Personpost.Adresser.Utlandsadress.Utdelningsadress1 : null,
        ForeignDistrubtionAddress2 = x.Personpost.Adresser.Utlandsadress != null ? x.Personpost.Adresser.Utlandsadress.Utdelningsadress2 : null,
        ForeignDistrubtionAddress3 = x.Personpost.Adresser.Utlandsadress != null ? x.Personpost.Adresser.Utlandsadress.Utdelningsadress3 : null,
        NationalRegistrationDistributionAddress1 = x.Personpost.Adresser.Folkbokforingsadress != null ? x.Personpost.Adresser.Folkbokforingsadress.Utdelningsadress1 : null,
        NationalRegistrationDistributionAddress2 = x.Personpost.Adresser.Folkbokforingsadress != null ? x.Personpost.Adresser.Folkbokforingsadress.Utdelningsadress2 : null,
        NationalRegistrationPostCode = x.Personpost.Adresser.Folkbokforingsadress != null ? x.Personpost.Adresser.Folkbokforingsadress.PostNr : null,

        UnregistrationReason = x.Personpost.Avregistrering != null ? x.Personpost.Avregistrering.AvregistreringsorsakKod : null,
        UnregistrationDate = x.Personpost.Avregistrering != null ? x.Personpost.Avregistrering.Avregistreringsdatum : null,

        NationalRegistrationCity = x.Personpost.Adresser.Folkbokforingsadress != null ? x.Personpost.Adresser.Folkbokforingsadress.Postort : null,
        BirthCountyCode = x.Personpost.Fodelse.HemortSverige != null ? x.Personpost.Fodelse.HemortSverige.FodelselanKod : null,
        BirthParish = x.Personpost.Fodelse.HemortSverige != null ? x.Personpost.Fodelse.HemortSverige.Fodelseforsamling : null,
        CitizenshipDate = x.Personpost.Medborgarskap != null ? x.Personpost.Medborgarskap.Medborgarskapsdatum : null,
        GivenNameNumber = x.Personpost.Namn.Tilltalsnamnsmarkering != null ? x.Personpost.Namn.Tilltalsnamnsmarkering : null,
        NationalRegistrationDate = x.Personpost.Folkbokforing != null ? x.Personpost.Folkbokforing.Folkbokforingsdatum : null,


        ProtectedIdentity = x.Sekretessmarkering != null ? x.Sekretessmarkering : null, 

        CitizenshipCode = x.Personpost.Medborgarskap.MedborgarskapslandKod != null ? x.Personpost.Medborgarskap.MedborgarskapslandKod : null 

    });

正如您在上面的代码中看到的,我检查了null一次。例如ForeignDistrubtionAddress1 = x.Personpost.Adresser.Utlandsadress != null ? x.Personpost.Adresser.Utlandsadress.Utdelningsadress1 : null

在该示例中,它确保x.Personpost.Adresser.Utlandsadress在继续之前不是null。但是,如果我还想在继续之前确保x.Personpost.Adresser不是null,该怎么办?

在分配值之前,有没有人知道如何检查null两次?

谢谢

3 个答案:

答案 0 :(得分:2)

您可以使用Null-conditional Operators (C# and Visual Basic)引入的c# 6.0

即:

var a = x?.Personpost?.Adresser?.Utlandsadress;

是简短形式:

if(x != null && x.Personpost != null && x.Personpost.Adresser != null)
    var a = x.Personpost.Adresser.Utlandsadress;
else
    var a = null;

首先会检查x != null,如果不null,则检查Personpost是否null等等。如果其中一个为null,则会将null分配给a

修改

如果您要将a的默认值指定为null,则可以另外使用??运算符:

var defaultValue = "someValue";
var a = x?.Personpost?.Adresser?.Utlandsadress ?? defaultValue;

答案 1 :(得分:1)

ForeignDistrubtionAddress1 = (x.Personpost.Adresser!= null 
 && x.Personpost.Adresser.Utlandsadress != null) 
 ? x.Personpost.Adresser.Utlandsadress.Utdelningsadress1 
 : null

答案 2 :(得分:1)

如果您使用的是C#6,则可以使用null conditional operator

x.Personpost?.Adresser?.Utlandsadress