仅当多个孔在属性中不为零时,才会自动映射多个孔

本文关键字:零时 映射 属性 | 更新日期: 2023-09-27 18:34:14

我必须像字符串一样返回CallerAddress属性。从多个属性进行连接,除非它们不为 null。到目前为止,我已经尝试过这个,但没有工作。映射后,我的来电地址等于"Str. SomeStreetName"。我在我的数据库中查找了一下,我在其他列上有值。那么,我怎样才能使它工作呢?

 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<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<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)))));