Question Automapper How to set another property if source is null

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
287
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("-"));
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,643
Location
Sydney, Australia
Programming Experience
10+
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);
 
Top Bottom