public static DataTable ListToDataTable<T>(IEnumerable<T> list)
{
DataTable dt = new DataTable();
List<PropertyInfo> properties = new List<PropertyInfo>();
foreach (PropertyInfo propInfo in typeof(T).GetProperties())
{
//cada propiedad va a ser una nueva columna del DataTable
properties.Add(propInfo);
DataColumn col = new DataColumn(propInfo.Name, propInfo.PropertyType);
dt.Columns.Add(col);
}
foreach (T item in list)
{
DataRow row = dt.NewRow();
foreach (PropertyInfo prop in properties)
{
row[prop.Name] = prop.GetValue(item, null);
}
dt.Rows.Add(row);
}
return dt;
}
Saludos,
Mike
No hay comentarios:
Publicar un comentario