AutoMapper Map If Not Null, Otherwise Custom Convert

前端 未结 4 1221
伪装坚强ぢ
伪装坚强ぢ 2021-02-02 08:29

Here\'s my code:

Mapper.CreateMap()
   .ForMember(dest => dest.Bar, opt => opt.MapFrom(src => src.Bar == null ? new BarViewModel() : sr         


        
4条回答
  •  没有蜡笔的小新
    2021-02-02 08:49

    You can use custom value resolver. The following should work:

    Mapper.CreateMap()
       .ForMember(dest => dest.Bar, opt => opt.ResolveUsing(src => src.Bar == null ? new Bar() : Mapper.Map(src.Bar)))
    

提交回复
热议问题