private DataTable ConvertListToDataTable<T>(List<T> dataList)
{
DataTable dataTable =
new DataTable();
//Creazione delle colonne
typeof(T).GetProperties().ToList().ForEach(property =>
{
dataTable.Columns.Add(property.Name,
Nullable.GetUnderlyingType(property.PropertyType)
?? property.PropertyType);
});
//Valorizzo i dati nel datatable
dataList.ForEach(item =>
{
DataRow row = dataTable.NewRow();
typeof(T).GetProperties().ToList().ForEach(property =>
{
row[property.Name]
= property.GetValue(item) ?? DBNull.Value;
});
dataTable.Rows.Add(row);
});
return dataTable;
}
DataTable dati = ConvertListToDataTable(listDati);
Nessun commento:
Posta un commento