public class DataTableUtil
{
/// <summary>
/// 泛型集合转化为dataTable
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="list">集合</param>
/// <returns></returns>
public static DataTable ToDataTable<T>(IEnumerable<T> list)
{ //创建属性的集合
List<PropertyInfo> pList = new List<PropertyInfo>();
//获得反射的入口 Type type = typeof(T);
DataTable dt = new DataTable();
//把所有的public属性加入到集合 并添加DataTable的列
Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
foreach (var item in list)
{
//创建一个DataRow实例
DataRow row = dt.NewRow();
//给row 赋值
pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
//加入到DataTable
dt.Rows.Add(row);
}
return dt;
}
}