DEV Community

Ola Johansson
Ola Johansson

Posted on

1

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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay