-2

Working on a legacy application where Data Access repository always returns a data set. Changing to any ORM frameworks is not an option for me at this point, looking for best options to map result set to a CLR type

pnuts
  • 58,317
  • 11
  • 87
  • 139
Kiran Vedula
  • 553
  • 6
  • 14

1 Answers1

0

I know of 2 easy ways to do this.

1 - Use Dapper.NET

const string query = "SELECT * FROM Users";
return connection.Query<User>(query);

With Dapper you don't even have to worry about getting the DataTable, just query the SqlConnection and get your type back.

2 - Use Automapper:

List<User> users = AutoMapper.Mapper.DynamicMap<IDataReader, List<User>>(
sourceDataTable.CreateDataReader());

Automapper can map your DataTable to your type.

Both methods require your class to have property names that match the column names in your source data.

I think they are both available as NuGet packages

Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
  • Thank you Glen. Type's property names do not match with Database column names, apparently, automapper has create mapper to define mappings. I will explore the options suggested and will update this thread – Kiran Vedula Sep 14 '15 at 01:46
  • 1
    Yes automapper allows you to define the mappings. Apparently this can also be achieved with Dapper. See [this article](http://stackoverflow.com/a/12615036/5152001). Given the choice I would probably go with Dapper as it is specially designed for this purpose and will almost certainly require less code. I'm not sure how well automapper caters for DataTable mappings. – Glen Thomas Sep 14 '15 at 01:54