A Coder
A Coder

Reputation: 3046

Update destination property in Automapper

I have 2 properties in the Source class and that needs to be combined and updated in the Destination class.

I tried like below and I had the destination namespace as the value and not the actual value.

//Code

    CreateMap<Source, Destination>().ForMember(x => x.Name, opt => opt.MapFrom(y => new Destination { Name = y.FirstName + y.LastName }));

I need the FirstName & LastName property from the source to be combined and set to Name property in Destiantion class.

Where am I wrong?

Upvotes: 1

Views: 133

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205769

Inside your ForMember call, x is of type Destination and y is of type Source (that's why they usually are named dst and src). The destination member is provided by the first expression (x.Name), so inside MapFrom you just need to provide the source expression to be assigned to that member:

CreateMap<Source, Destination>()
    .ForMember(dst => dst.Name, opt => opt.MapFrom(src => src.FirstName + src.LastName));

Upvotes: 3

Related Questions