0
votes

If I have the following classes:

public MainModel
{
    public List<ChildModel> Children {get; set;}
}

public ChildModel
{
    public bool IsDifferent {get; set;}
}

public MainDto
{
    public List<ChildDto> Children {get; set;}
    public List<DifferentChildDto> Different {get; set;}
}

public ChildDto
{ }

public DifferentChildDto
{ }

Using AutoMapper, is it possible to split and map the ChildModel list into 2 separate lists based on the property?

The end result shoud be that items with IsDifferent property set will be in the Different list whilst the remaining items are in the Children list.

The mapping should also work in reverse, ie. Merge the two DTO lists into the 1 model list.

1
Regarding the reverse mapping: Lists are defined as ordered collections, so how do you expect the order of items for the merged list? - grek40
@grek40 Order is unimportant. - Ivan-Mark Debono

1 Answers

0
votes
Mapper.CreateMap<MainModel, MainDto>()
    .ForMember(d => d.Children, o => o.MapFrom(s => s.Children.Where(c => !c.IsDifferent)))
    .ForMember(d => d.Different, o => o.MapFrom(s => s.Children.Where(c => c.IsDifferent)));

Mapper.CreateMap<ChildModel, ChildDto>();

Mapper.CreateMap<ChildModel, DifferentChildDto>();

Reversing this is not as trivial, you might be better off posting that as a separate question