It’s always a pain to work with DataTables and DataSets. You’re always typing these pesky column names, it doesn’t necessarily represents the actual data model (if there are nested data), there is no type safety until runtime, it’s pretty hard to use Linq on your results… the list goes on.
Fortunately, there is a simple solution: AutoMapper. It’s very simple to cast a DataTable to a list of objects:
1 2 3 4 5 6 7 8 |
public class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } List<Person> people = AutoMapper.Mapper.DynamicMap<IDataReader, List<Person>>( sourceDataTable.CreateDataReader()); |