使用 AutoMapper 将 DataTable 映射到对象 (DTO)

Using AutoMapper to Map a DataTable to an Object (DTO)

我正在尝试使用 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 return 有 10 行,但是当您查看 apiObject 时,它 return 没有行,这似乎没有任何意义。我已经研究了一段时间了,谷歌搜索后看起来我做对了。

当 return 映射到 dtoAPISimpleInvestor

时,正确的列在 dt 中

有人可以帮我吗?

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

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<DataRow> 的对象,可以使用以下语句从 DataTable 对象获取new List<DataRow>(dataTable.Rows.OfType<DataRow>());(因为 DataTable 对象的 Rows 属性 是一个实现了 IEnumerable 而不是 IEnumerable<T> 的集合)。

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

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

这对我有用: 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);