There are many cases where we will have to convert Dataset into list of objects. Below is a generic method using reflection to achieve that.
Below will work only if datatable column name and class property name are same and they match exactly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| using System.Reflection
internal static List<T> ConvertDataTableToList<T>(DataTable dt)
{
List<T> data = new List<T>();
foreach (DataRow row in dt.Rows)
{
T item = GetItem<T>(row);
data.Add(item);
}
return data;
}
internal static T GetItem<T>(DataRow dr)
{
Type temp = typeof(T);
T obj = Activator.CreateInstance<T>();
foreach (DataColumn column in dr.Table.Columns)
{
foreach (PropertyInfo pro in temp.GetProperties())
{
if (pro.Name == column.ColumnName)
pro.SetValue(obj, dr[column.ColumnName], null);
else
continue;
}
}
return obj;
}
|
Usage of this will be like below
1
2
3
| orderDetailsList = ConvertDataTable< OrderDetails >(orderdetailDatatable);
// orderdetailDataset.Table[0] can be used
|