使用.Select()多次检查null

西蒙·S

我正在一个项目中,在该项目中反序列化XML,然后将其读取到数据库中。我能够做到这一点,只有一个问题。问题是我希望能够null在反序列化XML之前对其进行两次检查现在我只能检查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在分配值之前进行两次检查吗?

谢谢

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用SQL查询多次重复<select>菜单

检查是否已使用不同的参数多次调用函数

使用SELECT WHERE NULL更新

在 SELECT 中使用 SELECT 返回 null

使用@NonNull检查input == null与抛出NPE

使用jQuery检查html select的值

使用Go的select语句仔细检查

在Oracle检查约束中使用select语句

使用 DataTable.Select 检查空值

使用Select的Python套接字检查数据

多次使用“ as”

检查列表为null时更好:不为null或使用Any

如果参数为null,如何检查是否为null,然后使用IS NULL检查值

在使用 send() 之前使用 select() 检查套接字

多次使用preg_match检查字符串是否在文本中

量角器 - 是否可以使用单个“期望”语句对元素进行多次检查?

使用charAt()检查字符是否在字符串中多次出现并循环

尝试使用 或 在 ng-if 指令中缩短检查条件。多次比较系列值,因为有许多值要检查或

使用可选检查访问内部JSON字段null

如何使用AngularJS检查数组是否为NULL?

使用db null检查初始化的对象

如果使用其他可选内容,则重写null检查

在Java中使用可选的多null检查8

使用严格的null检查的RegExpMatchArray的打字稿类型定义

使用异常代替冗长的null检查是否可以接受?

使用库检查null是否比获得NPE更好?

使用MySqlDataReader检查Mysql表行是否为NULL

SQL Server:检查数据(如果可用),否则使用IS NULL

使用isEmpty()检查字符串是否为null