2010-11-25 115 views
38

我有以下Automapper确定指标:Automapper - 它是否映射对象列表?

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()); 

当我映射一个对象也能正常工作。但我似乎无法通过对象列表。在列表中传递时是否需要不同的定义,还是不可能?

+0

为什么你有相同的映射两次?你应该只定义一次(大概是第二个) – BeRecursive 2010-11-25 13:43:10

+5

@BeRecursive - 可能是因为我对这个工具总共有2个小时的经验。 – 2010-11-25 14:46:28

回答

111

在你AutoMapper定义:

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);