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

I am trying to map a DataTable to an object (DTO) using the AutoMappers DynamicMap function.

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 returns 10 lines, but when you look at apiObject, it does not return lines, and this does not seem to make any sense. I have been looking at this for a while now, and after searching the Internet, it looks like I'm doing it right.

The correct columns are in dt when its return, which is displayed in dtoAPISimpleInvestor

Can someone please help me?

+7
c # automapper
source share
1 answer

How about the following ...

AutoMapper Profile

 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; // } } 

NOTE. I use the DataRow type as the source, not IDataReader (more on this below).

Profile Usage

 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); 

The result object must contain the correct number of dtoAPISimpleInvestor objects with the correct data.

NOTE. Calling mapper.Map calls an object of type List<DataRow> , which can be obtained from the DataTable using the new List<DataRow>(dataTable.Rows.OfType<DataRow>()); (since the Rows property of a DataTable is a collection that implements IEnumerable but not IEnumerable<T> ).

This is most likely not the only solution, but I confirmed that it works.

As a side note, I noticed that the DynamicMap method that you referenced was marked as deprecated in the latest version of the library, so you can avoid using it.

+11
source share

All Articles