Automapper Datatable to List

时间:2016-04-21 13:53:42

标签: c# datatable automapper

我想使用automapper将我的数据表映射到List。我尝试使用下面的代码,但它没有工作,我收到以下错误。

var data = GetDataTableResult();
var result = Mapper.Map<IDataReader, IList<RollCall>(data.CreateDataReader()); 

public class RollCall
{
    public long Id { get; set; }
    public string CreationUserId { get; set; }
    public DateTime CreationTs { get; set; }
    public string LastChangeUserId { get; set; }
    public DateTime LastChangeTs { get; set; }
    public byte StatusType { get; set; }
    public DateTime EntryTs { get; set; }
    public int ClientId { get; set; }
    public int RollcallId { get; set; }
    public int VehicleId { get; set; }
    public double Quantity { get; set; }
    public double OpeningOdo { get; set; }
    public double ClosingOdo { get; set; }
    public int OrderId { get; set; }
}

缺少类型映射配置或不支持的映射。 映射类型: IDataReader - &gt; IList 1 System.Data.IDataReader -> System.Collections.Generic.IList 1 [[SampleDemo.RollCall,SampleDemo,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]] 目的地路径: IList`1 来源价值: System.Data.DataTableReader

1 个答案:

答案 0 :(得分:0)

首先重置映射器并创建映射,然后执行映射器并将IList更改为List:

AutoMapper.Mapper.Reset();
AutoMapper.Mapper.CreateMap<IDataReader, RollCall>();
result = AutoMapper.Mapper.Map<IDataReader, List<RollCall>>(data.CreateDataReader());