markus / ConvertService / ServiceBase / Markus.Service.DataBase.Dapper / Extensions.cs @ f333dcc2
이력 | 보기 | 이력해설 | 다운로드 (1003 Bytes)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.ComponentModel; |
4 |
using System.Data; |
5 |
using System.Linq; |
6 |
using System.Text; |
7 |
using System.Threading.Tasks; |
8 |
|
9 |
namespace Markus.Service.DataBase |
10 |
{ |
11 |
public static class Util |
12 |
{ |
13 |
public static DataTable ToDataTable<T>(this IEnumerable<T> data,string Name = null) |
14 |
{ |
15 |
PropertyDescriptorCollection properties = |
16 |
TypeDescriptor.GetProperties(typeof(T)); |
17 |
DataTable table = new DataTable(); |
18 |
foreach (PropertyDescriptor prop in properties) |
19 |
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); |
20 |
foreach (T item in data) |
21 |
{ |
22 |
DataRow row = table.NewRow(); |
23 |
foreach (PropertyDescriptor prop in properties) |
24 |
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; |
25 |
table.Rows.Add(row); |
26 |
} |
27 |
return table; |
28 |
} |
29 |
} |
30 |
} |