I am using ASP.NET Web API and C#. As i am new to Express Mapper and i am having ADO.NET code which is returning list of results.How to map using Express Mapper?
Asked
Active
Viewed 831 times
-1
-
you could implement a custom `ICustomTypeMapper
` – Amit Kumar Ghosh Oct 12 '16 at 07:17 -
Can u explain me by writting more lines of code.because i am totally new to this express mapper topic – Sai Kumar Oct 12 '16 at 07:24
-
Oh, that's your home work. – Amit Kumar Ghosh Oct 12 '16 at 07:55
1 Answers
1
This is a test to demonstrate how to work with custom mappers in ExpressMapper. I hope you'll be able to use it accordingly -
public static void Main()
{
var ds = new DataSet();
var dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Age", typeof(int));
dt.Rows.Add("Test", 10);
dt.Rows.Add("Test2", 10);
ds.Tables.Add(dt);
var mapped = Mapper.Map<DataTable, List<RequestModel>>(ds.Tables[0], new CustomTypeMapper());
}
where -
class RequestModel
{
public int Age { get; set; }
public string Name { get; set; }
}
class CustomTypeMapper : ICustomTypeMapper<DataTable, List<RequestModel>>
{
public List<RequestModel> Map(IMappingContext<DataTable, List<RequestModel>> context)
{
if (context.Source == null)
throw new ArgumentNullException();
var output = new List<RequestModel>();
foreach (DataRow row in context.Source.Rows)
{
output.Add(new RequestModel
{
Age = row.Field<int>("Age"),
Name = row.Field<string>("Name")
});
}
return output;
}
}

Amit Kumar Ghosh
- 3,618
- 1
- 20
- 24