使用AutoMapper将数据表映射到对象(DTO) [英] Using AutoMapper to Map a DataTable to an Object (DTO)
问题描述
我正在尝试使用AutoMappers DynamicMap功能将DataTable映射到对象(DTO).
I am trying to map a DataTable to an object (DTO) using AutoMappers DynamicMap feature.
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时,它不返回任何行,这似乎没有任何意义.我已经看了一段时间了,在谷歌搜索之后,看来我做得正确.
dt
returns 10 rows but when you look at the apiObject it returns no rows and this does not seem to make any sense. I have been looking at this for a while now and after googling it looks like I am doing it correctly.
返回时,正确的列位于dt中,该列映射到dtoAPISimpleInvestor
The correct columns are in the dt when its return which map to the dtoAPISimpleInvestor
有人可以帮我吗?
推荐答案
类似以下内容...
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
作为源(
NOTE : I am using the DataRow
type as the source and not IDataReader
(more on this below).
使用个人资料
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
对象以及正确的数据.
The result
object should contain the correct number of dtoAPISimpleInvestor
objects with the correct data.
注意:对mapper.Map
的调用采用类型为List<DataRow>
的对象,可以使用语句new List<DataRow>(dataTable.Rows.OfType<DataRow>());
从DataTable
对象获得该对象(因为new List<DataRow>(dataTable.Rows.OfType<DataRow>());
的Rows
属性DataTable
对象是实现IEnumerable
而不是IEnumerable<T>
的集合).
NOTE : The call to mapper.Map
takes an object of type List<DataRow>
which can be obtained from the DataTable
object using the statement new List<DataRow>(dataTable.Rows.OfType<DataRow>());
(since the Rows
property of the DataTable
object is a collection that implements IEnumerable
but not IEnumerable<T>
).
这可能不是 only 解决方案,但我已经验证了它的有效性.
This is likely not the only solution but I have validated that it works.
作为旁注,我注意到您引用的DynamicMap
方法在该库的最新版本中已被标记为过时,因此您可能要避免使用它.
As a side note, I noticed that DynamicMap
method that you referenced has been marked as obsolete in the latest version of the library so you may want to avoid using it.
这篇关于使用AutoMapper将数据表映射到对象(DTO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!