【问题标题】:AutoMapper Return an empty list when Mapping two listsAutoMapper 映射两个列表时返回一个空列表
【发布时间】:2016-10-05 23:23:49
【问题描述】:

我想使用 AutoMapper 将一个列表转换为另一个列表,但它返回一个空列表

这是我的源对象

public class Charge
{
    public int ChargeID { get; set; }
    public string ChargeName { get; set; }
    public int CreatedBy { get; set; }
    public DateTime CreatedDate { get; set; }
    public int ModifiedBy { get; set; }
    public DateTime ModifiedDate { get; set; }
}

这是我的目标对象

public class ChargeVM
{
    public int ChargeID { get; set; }
    public string ChargeName { get; set; }
    public bool IsActive { get; set; }
}

这是我的 AutoMapper 映射

 List<Charge> chargeList= GetActiveChargeTemplates();   

 Mapper.CreateMap<List<Charge>, List<ChargeVM>>();

 List<ChargeVM>  list=Mapper.Map<List<Charge>, List<ChargeVM>>(chargeList);

此返回列表为 count=0 的空列表。
我在这里做错了吗?

【问题讨论】:

    标签: c# automapper


    【解决方案1】:

    改变

    Mapper.CreateMap<List<Charge>, List<ChargeVM>>();
    

    Mapper.CreateMap<Charge, ChargeVM>();
    

    您不必为列表显式创建地图,只需为列表中的对象创建。

    【讨论】:

    • 非常感谢 HansVG 。你拯救了我的一天
    猜你喜欢
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    • 2018-03-21
    • 1970-01-01
    相关资源
    最近更新 更多