【问题标题】:Using AutoMapper to Map a DataTable to an Object (DTO)使用 AutoMapper 将 DataTable 映射到对象 (DTO)
【发布时间】:2016-02-15 16:23:06
【问题描述】:

我正在尝试使用 AutoMappers DynamicMap 功能将 DataTable 映射到对象 (DTO)。

DataTable dt;
dt = new dalAllInvestors().InvestorNameSearch(investorNameSearch);

// Look at DynamicMap - Urgent 
List<dtoAPISimpleInvestor> apiObject = AutoMapper.Mapper.DynamicMap<IDataReader, List<dtoAPISimpleInvestor>>(
dt.CreateDataReader());

return apiObject;


public class dtoAPISimpleInvestor
{
    public int FirmID { get; set; }
    public string FirmName { get; set; }
    public string Type { get; set; }
    public string Location { get; set; }
}

dt 返回 10 行,但是当您查看 apiObject 时,它没有返回任何行,这似乎没有任何意义。我已经看了一段时间了,谷歌搜索后看起来我做得对。

当它返回映射到dtoAPISimpleInvestor时,正确的列在dt中

有人可以帮帮我吗?

【问题讨论】:

    标签: c# automapper


    【解决方案1】:

    像下面这样的东西怎么样......

    AutoMapper 配置文件

    public sealed class SimpleInvestorProfile : Profile
    {
      // This is the approach starting with version 5
      public SimpleInvestorProfile()
      {
          IMappingExpression<DataRow, dtoAPISimpleInvestor> mappingExpression;
    
        mappingExpression = CreateMap<DataRow, dtoAPISimpleInvestor>();
        mappingExpression.ForMember(d => d.FirmID, o => o.MapFrom(s => s["FirmID"]));
        mappingExpression.ForMember(d => d.FirmName, o => o.MapFrom(s => s["FirmName"]));
        mappingExpression.ForMember(d => d.Type, o => o.MapFrom(s => s["Type"]));
        mappingExpression.ForMember(d => d.Location, o => o.MapFrom(s => s["Location"]));
    
      }
    
      // this method is obsolete in version 5
      // protected override void Configure()
      // {
      //   IMappingExpression<DataRow, dtoAPISimpleInvestor> mappingExpression;
    
      //  mappingExpression = CreateMap<DataRow, dtoAPISimpleInvestor>();
      //  mappingExpression.ForMember(d => d.FirmID, o => o.MapFrom(s => s["FirmID"]));
      //  mappingExpression.ForMember(d => d.FirmName, o => o.MapFrom(s => s["FirmName"]));
      //   mappingExpression.ForMember(d => d.Type, o => o.MapFrom(s => s["Type"]));
      //  mappingExpression.ForMember(d => d.Location, o => o.MapFrom(s => s["Location"]));
    
      //  return;
     // }
    }
    

    注意:我使用 DataRow 类型作为源,而不是 IDataReader(更多内容见下文)。

    使用个人资料

    MapperConfiguration configuration;
    
    configuration = new MapperConfiguration(a => {a.AddProfile(new SimpleInvestorProfile());});
    
    IMapper mapper;
    
    mapper = configuration.CreateMapper();
    
    List<dtoAPISimpleInvestor> result;
    
    result = mapper.Map<List<DataRow>, List<dtoAPISimpleInvestor>>(rows);
    

    result 对象应包含正确数量的 dtoAPISimpleInvestor 对象和正确的数据。

    注意:对mapper.Map 的调用采用List&lt;DataRow&gt; 类型的对象,该对象可以使用语句new List&lt;DataRow&gt;(dataTable.Rows.OfType&lt;DataRow&gt;());DataTable 对象获得(因为Rows 的属性为DataTable 对象是一个实现了IEnumerable 但不是IEnumerable&lt;T&gt; 的集合。

    这可能不是唯一的解决方案,但我已经验证它有效。

    附带说明一下,我注意到您引用的 DynamicMap 方法在最新版本的库中已被标记为已过时,因此您可能希望避免使用它。

    【讨论】:

    • 我得到参数 1:无法从 'System.Data.DataRowCollection' 转换为 'System.Collections.Generic.List
    • 好的,这使用 result = mapper.Map, List>(yourdatatable.AsEnumerable().ToL‌​ist());
    • .AsEnumberable()source添加对System.Data.DataTableExtensions的引用
    【解决方案2】:

    这对我有用: automapper 的版本是 3.1.1 从 nuget 下载

    using AutoMapper;
    
    public List<T> ReadData<T>(DataTable dt)
    {            
      return Mapper.DynamicMap<IDataReader, List<T>>(dt.CreateDataReader());                        
    }
    

    调用方法如下:

    DataTable dt = getPeopleDT();
    List<PEOPLEDTO> peopleList = ReadData<PEOPLEDTO>(dt);   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 2018-03-19
      • 2014-10-21
      相关资源
      最近更新 更多