IncludeMembers in AutoMapper for mapping relations

olaj

Ola Johansson

Posted on March 22, 2023

IncludeMembers in AutoMapper for mapping relations

Sometimes you need to grab a value from a relations table, as in this case the sortorder of the specific relation between a "list" and an "item".

Using Automapper i have always had issues with this and usually just been lazy and mapped the other properties manually or used the AfterMap feature.

But today i found out about the IncludeMembers method. It's super simple and you just include the inner property and automapper will treat it just like it's "merged" with the actual source.

CreateMap<ListItemRelation, ItemForCustomList>()
   .IncludeMembers(s => s.Item)
   .ForMember(dest => dest.SortOrder, o => o.MapFrom(src => src.Sortorder)));
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
olaj
Ola Johansson

Posted on March 22, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related