using System.Data.DataSetExtensions;
using System.Collections.Generic;
public static void Main()
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("foo", typeof(DateTime));
dt.Columns.Add("foo1", typeof(DateTime));
dt.Columns.Add("foo2", typeof(DateTime));
dt.Columns.Add("foo3", typeof(Decimal));
dt.Columns.Add("foo4", typeof(DateTime));
dt.Columns.Add("foo5", typeof(DateTime));
dt.Columns.Add("foo6", typeof(DateTime));
dt.Columns.Add("foo7", typeof(DateTime));
dt.Columns.Add("foo8", typeof(Double));
dt.Columns.Add("foo9", typeof(bool));
for(int i=0;i<=10000;i++){dt.Rows.Add(4, "fafa", null, DateTime.Now, DateTime.Now, 1.1, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, 100.153 , true );}
var typedList = ToListof<Class1>(dt);
Console.WriteLine(typedList[0].id);
Console.WriteLine(typedList[0].name);
Console.WriteLine(typedList[0].foo);
Console.WriteLine(typedList[0].foo1);
public static List<T> ToListof<T>(DataTable dt)
const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
var columnNames = dt.Columns.Cast<DataColumn>()
.Select(c => c.ColumnName)
var objectProperties = typeof(T).GetProperties(flags);
var targetList = dt.AsEnumerable().Select(dataRow =>
var instanceOfT = Activator.CreateInstance<T>();
foreach (var properties in objectProperties.Where(properties => columnNames.Contains(properties.Name) && dataRow[properties.Name] != DBNull.Value))
properties.SetValue(instanceOfT, dataRow[properties.Name], null);
public int? id {get;set;}
public string name {get;set;}
public DateTime? foo {get;set;}
public DateTime foo1 {get;set;}
public DateTime foo2 {get;set;}
public Decimal foo3 {get;set;}
public DateTime foo4 {get;set;}
public DateTime foo5 {get;set;}
public DateTime foo6 {get;set;}
public DateTime foo7 {get;set;}
public Double foo8 {get;set;}
public bool foo9 {get;set;}