Hi,
I have another approach you might take a look.
It's a helper method. Create a custom class file named CollectionHelper:
public static IList<T> ConvertTo<T>(DataTable table)
{
if (table == null)
return null;
List<DataRow> rows = new List<DataRow>();
foreach (DataRow row in table.Rows)
rows.Add(row);
return ConvertTo<T>(rows);
}
Imagine you want to get a list of costumers. Now you'll had the following caller:
List<Customer> myList = (List<Customer>)CollectionHelper.ConvertTo<Customer>(table);
The attributes you have in your DataTable must match your Customer class (fields like Name, Address, Telephone).
I hope it helps!
For who are willing to know why to use lists instead of DataTables: http://stackoverflow.com/questions/275269/does-a-datatable-consume-more-memory-than-a-listt
The full sample:
http://lozanotek.com/blog/archive/2007/05/09/Converting_Custom_Collections_To_and_From_DataTable.aspx