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
TypeA
andTypeB
also. Aslo try onces.MapFrom(x => x.typeAList.Select(a => new typeB() { prop1 = a.prop1, prop2 = a.prop2 })
– Satpalcovertusing()
method? – Dhinesh