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

当我映射单个对象时,这工作得很好。但我似乎无法传递对象列表。传入列表时是否需要不同的定义,或者这是不可能的?

最佳答案

在您的 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);

关于Automapper - 它映射对象列表吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4276664/

相关文章:

C# AutoMapperMappingException KeyNotFoundException 异常

c# - EF 生成 System.Data.SqlTypes.SqlNullValueException : Data is Null. 无法对 Null 值调用此方法或属性

c# - Automapper 空属性

c# - AutoMapper map 中 foreach 的额外迭代

c# - Automapper 根据源类型中的枚举值解析目标类型

c# - 无法在单元测试中设置 AutoMapper,无法加载文件或程序集 Microsoft.AspNetCore.Mvc.ViewFeatures

c# - 在没有静态 API 的情况下在应用程序启动时配置 AutoMapper v4.2

c# - AutoMapper:从多个源属性映射到集合

c# - 升级到 automapper 3 后缺少方法

c# - 使用自动映射器忽略未使用的属性