Automapper-它是否映射对象列表?
问题描述:
我有以下自动映射器定义:
I have the following Automapper defintion:
Mapper.CreateMap<IB.BusinessComponents.Data.LocationMaster, IB.Entites.Master.Location>();
Mapper.CreateMap<IB.BusinessComponents.Data.LocationMaster, IB.Entites.Master.Location>()
.ForMember(destination => destination.Id, source => source.MapFrom(item => item.LocationMasterID))
.ForMember(destination => destination.ChildLocationList, source => source.Ignore());
当我映射单个对象时,这可以正常工作.但是我似乎无法传递对象列表.传递列表时是否需要其他定义,还是不可能?
This works fine when I map a single object. But I can't seem to pass in Lists of objects. Do I need a different definition when passing in a list, or is it not possible?
答
在您的AutoMapper定义中:
In your AutoMapper Definition:
CreateMap<MyStuffDTO, MyStuffViewModel>()
.ForMember(dto => dto.MyDate, opt => opt.MapFrom(src => src.LastDate))
.ForMember(dto => dto.MyTime, opt => opt.MapFrom(src => src.LastTime))
.ForMember(dto => dto.Category, opt => opt.MapFrom(src => src.Category));
在代码中:
单身:
var result = Mapper.Map<MyStuffDTO, MyStuffViewModel>(obj);
对于列表:
var list = Mapper.Map<IList<MyStuffDTO>, IList<MyStuffViewModel>>(obj);