I'm using Mapper.DynamicMap()
inside a generic method and would like to, without using .CreateMap()
, ignore some any source values that are null. Is this even possible?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.
回答2:
I solved it with DataMember property in destination type [DataMember(EmitDefaultValue = false)]
add this in the destination DTO