public static class DataTableExtension
{
public static DataTable ToDataTable<T>(this List<T> list)
{
Type type = typeof(T);
DataTable dt = new DataTable(type.Name);
var propertyInfos = type.GetProperties().ToList();
//For each property of generic List (T), add a column to table
propertyInfos.ForEach(propertyInfo =>
{
Type columnType = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;
dt.Columns.Add(propertyInfo.Name, columnType);
});
//Visit every property of generic List (T) and add each value to the data table
list.ForEach(item =>
{
DataRow row = dt.NewRow();
propertyInfos.ForEach(
propertyInfo =>
row[propertyInfo.Name] = propertyInfo.GetValue(item, null) ?? DBNull.Value
);
dt.Rows.Add(row);
});
//Return the datatable
return dt;
}
}