我试图忽略一些成员并对当前成员提出一些条件:
CreateMap<UpdateProfileViewModel, UserSettingModel>()
.ForMember(dest => dest.FirstName, opts => opts.MapFrom(src => src.FirstName))
.ForMember(dest => dest.LastName, opts => opts.MapFrom(src => src.LastName))
.ForMember(dest => dest.PhoneNumber, opts => opts.MapFrom(src => src.PhoneNumber))
.ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null))
.ForAllOtherMembers(opts => opts.Ignore());
ForAllMembers我不能使用,因为我已经使用了ForAllMembers。
如何忽略源中的空值,同时忽略所有其他成员进行映射?
答案 0 :(得分:0)
因此,由于ForAllMembers
并且ForAllOtherMembers
返回void
,我建议使用opts.Condition()
方法,尽管这种解决方案不是很优雅
CreateMap<UpdateProfileViewModel, UserSettingModel>()
.ForMember(dest => dest.FirstName, opts => { opts.Condition(p => p.FirstName != null); opts.MapFrom(p => p.FirstName); })
.ForMember(dest => dest.LastName, opts => { opts.Condition(p => p.LastName != null); opts.MapFrom(p => p.LastName); })
.ForMember(dest => dest.PhoneNumber, opts => { opts.Condition(p => p.PhoneNumber != null); opts.MapFrom(p => p.PhoneNumber); })
.ForAllOtherMembers(opts => opts.Ignore());