当映射两个列表时,AutoMapper返回一个空列表

3
我希望使用AutoMapper将一个List转换为另一个List,但它返回一个空列表。
这是我的源对象。
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);

这会将返回列表作为空列表,计数为0。
我在这里做错了什么吗?

1个回答

8

更改

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

to

Mapper.CreateMap<Charge, ChargeVM>();

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


非常感谢HansVG。你救了我的一天。 - Rama

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接