0
votes
Class SourceClass
{
    string name {get;set;}
    IList<TypeA> typeAList {get;set;}
}
Class DestinationClass
{
    string name {get;set;}
    IList<TypeB> typeBList {get;set;}
}

How to map this two class using automapper ?

Try 1:- Mapper.CreateMap<SourceClass, DestinationClass>().ForMember(d => d.typeBList, s => s.MapFrom(x => x.typeAList.Select(a => new typeB() { prop1 = a.prop1, prop2 = a.prop2 }).ToList<typeB>()));

I am getting runtime conversion error for this .

Try 2:- Mapper.CreateMap<typeB,typeA>().ForMember(d=>d.prop1,opt=>opt.MapFrom(s=>s.prop1)); Mapper.CreateMap<SourceClass, DestinationClass>().ForMember(d => d.typeBList, s => s.MapFrom(x => x.typeAList));

Error :- Missing type map configuration or unsupported mapping.

Mapping types: typeA -> typeB

typeA -> typeB

Destination path: typeB.typeB0[0]

Source value: typeA

2
You need to create map for TypeA and TypeB also. Aslo try once s.MapFrom(x => x.typeAList.Select(a => new typeB() { prop1 = a.prop1, prop2 = a.prop2 })Satpal
Could it be done using covertusing() method?Dhinesh

2 Answers

0
votes

you can used the following code for an auto mapper

 Mapper.CreateMap<SourceClass ,DestinationClass>();
0
votes

You need to map the top two types and the list element types:

Mapper.CreateMap<SourceClass, DestinationClass>();
Mapper.CreateMap<TypeA, TypeB>();

AutoMapper maps collection types automatically, but each element is mapped individually so you'll need to create maps for any element types.