Using DynamicMap() and ignore null source value

后端 未结 2 1949
無奈伤痛
無奈伤痛 2020-12-31 01:21

I\'m using Mapper.DynamicMap() inside a generic method and would like to, without using .CreateMap(), ignore some any source values that are null.

相关标签:
2条回答
  • 2020-12-31 01:32

    I solved it with DataMember property in destination type [DataMember(EmitDefaultValue = false)] add this in the destination DTO

    0 讨论(0)
  • 2020-12-31 01:33

    If you want all source properties with null values to be ignored you could use:

    Mapper.CreateMap<SourceType, DestinationType>()
                        .ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull));
    

    Otherwise, you can do something similar for each member. This will get quit tedious if there are a large number of properties.

    0 讨论(0)
提交回复
热议问题