-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataTableExtension.cs
34 lines (28 loc) · 927 Bytes
/
DataTableExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
namespace BulkOperations
{
public static class DataTableExtension
{
public static DataTable FromList<T>(this DataTable dataTable, IList<T> list)
{
var properties = TypeDescriptor.GetProperties(typeof(T));
foreach (PropertyDescriptor prop in properties)
{
dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}
foreach (var item in list)
{
var row = dataTable.NewRow();
foreach (PropertyDescriptor prop in properties)
{
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
}
dataTable.Rows.Add(row);
}
return dataTable;
}
}
}