DEV Community

Ola Johansson
Ola Johansson

Posted on

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

Top comments (0)