The following function takes in a System.Collections.Generic.List<T> and returns a System.Data.DataTable with the properties (via reflection) of T as columns.
public static System.Data.DataTable ListToDataTable<T>(
System.Collections.Generic.IList<T> elements)
{
System.Reflection.PropertyInfo[] arrPropInfo = typeof(T).GetProperties();
System.Data.DataTable dt = new DataTable();
System.Data.DataRow dr;
foreach (System.Reflection.PropertyInfo pInfo in arrPropInfo)
{
dt.Columns.Add(pInfo.Name, pInfo.PropertyType);
}
foreach(object elem in elements)
{
dr = dt.NewRow();
foreach (System.Reflection.PropertyInfo pInfo in arrPropInfo)
{
dr[pInfo.Name] = pInfo.GetValue(elem, null);
}
dt.Rows.Add(dr);
}
return dt;
}