检查反序列化值是否为null

时间:2016-05-17 09:47:26

标签: c#

我有以下代码:

 Console.WriteLine("Deserilizing FolkbokföringsPost");
 var myPersons = Deserialize<List<FolkbokforingspostTYPE>>()
     .Select(x => new Person
     {
          PersonalIdentityNumber = x.Personpost.PersonId.PersonNr,
          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,
          NationalRegistrationDate = x.Personpost.Folkbokforing != null ? x.Personpost.Folkbokforing.Folkbokforingsdatum : null,

          if(x.Personpost.Fodelse.OrtUtlandet != null) //<-- The name 'x' does not exist in the current context
          {
               ForeignBirthCity = x.Personpost.Fodelse.OrtUtlandet != null ? x.Personpost.Fodelse.OrtUtlandet.FodelseortUtland : null, //<-- The name 'ForeignBirthCity' does not exist in the current context
          }

          ProtectedIdentity = x.Sekretessmarkering != null ? x.Sekretessmarkering : null, //<-- The name 'ProtectedIdentity' does not exist in the current context
          CitizenshipCode = x.Personpost.Medborgarskap.MedborgarskapslandKod != null ? x.Personpost.Medborgarskap.MedborgarskapslandKod : null //<-- The name 'CitizenshipCode' does not exist in the current context
     });

我收到这些错误:

  

//&lt; - 当前上下文中不存在名称'CitizenshipCode'

     

//&lt; - 当前名称'ProtectedIdentity'不存在   上下文

     

//&lt; - 当前名称'ForeignBirthCity'不存在   上下文

     

//&lt; - 当前上下文中不存在名称“x”

为什么我会收到这些错误以及如何解决?

2 个答案:

答案 0 :(得分:0)

只需删除代码中的if语句,因为

而没有意义
ForeignBirthCity = x.Personpost.Fodelse.OrtUtlandet != null ? x.Personpost.Fodelse.OrtUtlandet.FodelseortUtland : null,

答案 1 :(得分:0)

请确保 FolkbokforingspostTYPE 类型的目标对象具有列表类型对象以对其进行反序列化。

应该是这样的:

 public class FolkbokforingspostTYPE
 {
  public List<Person> Person{get;set;}
 }

并且您的Person类也具有以下属性:

public class Person
 {
    CitizenshipCode
    ProtectedIdentity
    CitizenshipCode
 }

你的反序列化应该是这样的:

var obj= new List<FolkbokforingspostTYPE>(){
//intialize your object
}
var myPersons = Deserialize<List<FolkbokforingspostTYPE>>(obj);