AutoMapper map from the original subcollection to another collection

EDIT: The title is incorrect, I'm trying to map from the source list to the list of source source models.

I am having trouble trying to match the list to another one specified in the nested model. View and inconsistency. The problem is that I do not know how to make comparisons.

Here is my setup after unsuccessful matching attempts:

public class DestinationModel
{
    public DestinationNestedViewModel sestinationNestedViewModel { get; set; }
}

public class DestinationNestedViewModel
{
    public List<ItemModel> NestedList { get; set; }
}

public class SourceModel
{
    public List<Item> SourceList { get; set; }
}

Where Item and ItemModel already have a mapping defined between them

I can’t do that ...

Mapper.CreateMap<SourceModel, DestinationModel>()
.ForMember(d => d.DestinationNestedViewModel.NestedList,
    opt => opt.MapFrom(src => src.SourceList))

ERROR:

The expression 'd => d.DestinationNestedViewModel.NestedList' must be allowed for the top-level element. Parameter Name: lambdaExpression

Then I tried something like this:

.ForMember(d => d.DestinationNestedViewModel, 
 o => o.MapFrom(t => new DestinationNestedViewModel { NestedList = t.SourceList }))

   NestedList = t.SourceList. , ItemModel Item . .

?

+5
1

, - :

Mapper.CreateMap<Item, ItemModel>();

/* Create a mapping from Source to Destination, but map the nested property from 
   the source itself */
Mapper.CreateMap<SourceModel, DestinationModel>()
    .ForMember(dest => dest.DestinationNestedViewModel, opt => opt.MapFrom(src => src));

/* Then also create a mapping from Source to DestinationNestedViewModel: */
Mapper.CreateMap<SourceModel, DestinationNestedViewModel>()
    .ForMember(dest => dest.NestedList, opt => opt.MapFrom(src => src.SourceList));

, , Mapper.Map Source Destination:

Mapper.Map<SourceModel, DestinationModel>(source);
+11

All Articles