Class SourceClass
{
string name {get;set;}
IList<TypeA> typeAList {get;set;}
}
Class DestinationClass
{
string name {get;set;}
IList<TypeB> typeBList {get;set;}
}
如何映射這兩個類使用automapper?自動映射轉換從列表<typeA>到類與列表<typeB>
嘗試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>()));
我正在運行時轉換錯誤這一點。
嘗試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));
錯誤: - 缺少類型映射配置或不支持的映射。
映射類型: 的typeA - >的TypeB
的typeA - >的TypeB
目標路徑: typeB.typeB0 [0]
Source值: 的typeA
您還需要爲'TypeA'和'TypeB'創建映射。 aslo嘗試一次's.MapFrom(x => x.typeAList.Select(a => new typeB(){prop1 = a.prop1,prop2 = a.prop2})' – Satpal
可以使用'covertusing()方法? – Dhinesh