Question Automapper How to set another property if source is null

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
361
Programming Experience
10+
Hello,

I wonder if it is possible to set Serial to Pin, if Pin is null.
C#:
     CreateMap<CouponDto, Coupon>()
                     .ForMember(dest => dest.Pin, src => src.NullSubstitute("-"))
                     .ForMember(dest => dest.Serial, src => src.NullSubstitute("-"));
 
Do you mean actually mean set dest.Serial to src.Pin if src.Serial is null? If so then presumably that would be done like so:
C#:
.ForMember(dest => dest.Serial, src => src.NullSubstitute(src.Pin));
I haven't used that NullSubstitute method before so I probably would have used this:
C#:
.ForMember(dest => dest.Serial, src => src.Serial ?? src.Pin);
 
Back
Top Bottom