C# 检查反序列化的值是否为null
我有以下代码:C# 检查反序列化的值是否为null,c#,C#,我有以下代码: Console.WriteLine("Deserilizing FolkbokföringsPost"); var myPersons = Deserialize<List<FolkbokforingspostTYPE>>() .Select(x => new Person { PersonalIdentityNumber = x.Personpost.PersonId.PersonNr,
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
});
Console.WriteLine(“反序列化FolkbokföringsPost”);
var myPersons=反序列化()
.选择(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.FolkBookForing.LanKod!=null?x.Personpost.FolkBookForing.LanKod:null,
NationalRegistration市政代码=x.Personpost.FolkBookForing.KommunKod!=null?x.Personpost.FolkBookForing.KommunKod:null,
ForeignDistributionAddress1=x.Personpost.Addresser.UtlandsAddress!=null?x.Personpost.Addresser.UtlandsAddress1:null,
ForeignDistributionAddress2=x.Personpost.Adresser.UtlandsAddress!=null?x.Personpost.Adresser.UtlandsAddress2:null,
ForeignDistributionAddress3=x.Personpost.Adresser.UtlandsAddress!=null?x.Personpost.Adresser.UtlandsAddress3:null,
NationalRegistrationDistributionAddress1=x.Personpost.Addresser.FolkBookForingsAddress!=null?x.Personpost.Addresser.FolkBookForingsAddress.UtdelningsAddress1:null,
NationalRegistrationDistributionAddress2=x.Personpost.Addresser.FolkBookForingsAddress!=null?x.Personpost.Addresser.FolkBookForingsAddress.UTDelningsAddress2:null,
NationalRegistrationPostCode=x.Personpost.Addresser.FolkBookForingsAddress!=null?x.Personpost.Addresser.FolkBookForingsAddress.PostNr:null,
UnregistrationReason=x.Personpost.Avregistrering!=null?x.Personpost.Avregistrering.AvRegistreringOrsakkod:null,
UnregistrationDate=x.Personpost.Avregistrering!=null?x.Personpost.Avregistrering.AvRegistreringDatum:null,
NationalRegistrationCity=x.Personpost.Addresser.FolkBookForingsAddress!=null?x.Personpost.Addresser.FolkBookForingsAddress.Postort:null,
NationalRegistrationDate=x.Personpost.FolkBookForing!=null?x.Personpost.FolkBookForing.FolkBookForingsDatum:null,
如果(x.Personpost.Fodelse.OrtUtlandet!=null)/只需从代码中删除if语句,则由于
ForeignBirthCity = x.Personpost.Fodelse.OrtUtlandet != null ? x.Personpost.Fodelse.OrtUtlandet.FodelseortUtland : null,
请确保您的FolkBookForingsPostType类型的目标对象具有要反序列化的列表对象
应该是这样的:
public class FolkbokforingspostTYPE
{
public List<Person> Person{get;set;}
}
您的反序列化应该是这样的:
var obj= new List<FolkbokforingspostTYPE>(){
//intialize your object
}
var myPersons = Deserialize<List<FolkbokforingspostTYPE>>(obj);
var obj=新列表(){
//初始化对象
}
var myPersons=反序列化(obj);
查看类FolkBookForingsPostType的代码会很有帮助
如果Select()
代码中有语句,则无法写入!
var obj= new List<FolkbokforingspostTYPE>(){
//intialize your object
}
var myPersons = Deserialize<List<FolkbokforingspostTYPE>>(obj);