我想使用 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; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的目标对象
public class ChargeVM
{
public int ChargeID { get; set; }
public string ChargeName { get; set; }
public bool IsActive { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的 AutoMapper 映射
List<Charge> chargeList= GetActiveChargeTemplates();
Mapper.CreateMap<List<Charge>, List<ChargeVM>>();
List<ChargeVM> list=Mapper.Map<List<Charge>, List<ChargeVM>>(chargeList);
Run Code Online (Sandbox Code Playgroud)
此返回列表作为计数 = 0 的空列表。
我在这里做错了吗?
改变
Mapper.CreateMap<List<Charge>, List<ChargeVM>>();
Run Code Online (Sandbox Code Playgroud)
到
Mapper.CreateMap<Charge, ChargeVM>();
Run Code Online (Sandbox Code Playgroud)
您不必为列表显式创建映射,只需为列表中的对象创建映射。
| 归档时间: |
|
| 查看次数: |
2787 次 |
| 最近记录: |