C# 仅当属性上的多个porperties不为null时,才自动映射它们
我必须从多个属性返回CallerAddress属性,就像string.Concat一样,除非它们不为null。到目前为止,我已经试过了,但不起作用。映射后,我的CallerAddress等于“Str.SomeStreetName”。我在数据库里查过了,其他列上有值。那么,我怎样才能让它发挥作用呢C# 仅当属性上的多个porperties不为null时,才自动映射它们,c#,automapper,C#,Automapper,我必须从多个属性返回CallerAddress属性,就像string.Concat一样,除非它们不为null。到目前为止,我已经试过了,但不起作用。映射后,我的CallerAddress等于“Str.SomeStreetName”。我在数据库里查过了,其他列上有值。那么,我怎样才能让它发挥作用呢 Mapper.CreateMap<ClientRecordingsDao,ClientRecording>() .ForMember(x => x.CallerAddress
Mapper.CreateMap<ClientRecordingsDao,ClientRecording>()
.ForMember(x => x.CallerAddress,
b =>
b.MapFrom(
x => x.CallerStreet != String.Empty
? "Str." + x.CallerStreet
: String.Empty +
x.CallerStreetNumber != String.Empty
? " Nr."
: String.Empty + x.CallerStreetNumber +
x.CallerBuilding != String.Empty
? " Bl."
: String.Empty + x.CallerBuilding +
x.CallerApartment != String.Empty
? " Ap."
: String.Empty + x.CallerApartment))
Mapper.CreateMap()
.ForMember(x=>x.calledAddress,
b=>
b、 映射自(
x=>x.CallerStreet!=String.Empty
?“Str.”+x.CallerStreet
:String.Empty+
x、 CallerStreetNumber!=字符串。空
?“编号”
:String.Empty+x.CallerStreetNumber+
x、 CallerBuilding!=字符串。空
“Bl”
:String.Empty+x.CallerBuilding+
x、 CallerPartment!=字符串。空
“美联社。”
:String.Empty+x.CallerApartment)
它将+
运算符应用于错误的位置。在()
中包装每个比较:
Mapper.CreateMap()
.ForMember(x=>x.calledAddress,b=>b.MapFrom(
x=>(x.CallerStreet!=String.Empty?“Str.”+x.CallerStreet:String.Empty)+
(x.CallerStreetNumber!=String.Empty?“编号”+x.CallerStreetNumber:String.Empty)+
(x.CallerBuilding!=String.Empty?“Bl.”+x.CallerBuilding:String.Empty)+
(x.CallerApartment!=String.Empty?“Ap.”+x.CallerApartment:String.Empty));
您的代码映射到此:
Mapper.CreateMap<ClientRecordingsDao,ClientRecording>()
.ForMember(x => x.CallerAddress, b => b.MapFrom(
x => x.CallerStreet != String.Empty ? "Str." + x.CallerStreet :
(String.Empty + x.CallerStreetNumber != String.Empty ? " Nr." + x.CallerStreetNumber :
(String.Empty + x.CallerBuilding != String.Empty ? " Bl." + x.CallerBuilding :
(String.Empty + x.CallerApartment != String.Empty ? " Ap." + x.CallerApartment : String.Empty)))));
Mapper.CreateMap()
.ForMember(x=>x.calledAddress,b=>b.MapFrom(
x=>x.CallerStreet!=String.Empty?“Str.”+x.CallerStreet:
(String.Empty+x.CallerStreetNumber!=String.Empty?“编号”+x.CallerStreetNumber:
(String.Empty+x.CallerBuilding!=String.Empty?“Bl.”+x.CallerBuilding:
(String.Empty+x.CallerApartment!=String.Empty?“Ap.+x.CallerApartment:String.Empty(()()));
Mapper.CreateMap<ClientRecordingsDao,ClientRecording>()
.ForMember(x => x.CallerAddress, b => b.MapFrom(
x => x.CallerStreet != String.Empty ? "Str." + x.CallerStreet :
(String.Empty + x.CallerStreetNumber != String.Empty ? " Nr." + x.CallerStreetNumber :
(String.Empty + x.CallerBuilding != String.Empty ? " Bl." + x.CallerBuilding :
(String.Empty + x.CallerApartment != String.Empty ? " Ap." + x.CallerApartment : String.Empty)))));