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
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.
The correct columns are in the dt when its return which map to the dtoAPISimpleInvestor
Can somebody please help me?
This worked for me: Version of automapper is 3.1.1 download from nugget
Call method like this:
How about something like the following...
AutoMapper Profile
NOTE : I am using the
DataRow
type as the source and notIDataReader
(more on this below).Using the Profile
The
result
object should contain the correct number ofdtoAPISimpleInvestor
objects with the correct data.NOTE : The call to
mapper.Map
takes an object of typeList<DataRow>
which can be obtained from theDataTable
object using the statementnew List<DataRow>(dataTable.Rows.OfType<DataRow>());
(since theRows
property of theDataTable
object is a collection that implementsIEnumerable
but notIEnumerable<T>
).This is likely not the only solution but I have validated that it works.
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.