forked from laochiangx/Common.Utility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataTableExtensions.cs
109 lines (100 loc) · 5 KB
/
DataTableExtensions.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace Common.Utilities
{
public static class DataTableExtensions
{
public static T ToEntity<T>(this DataTable table) where T : new()
{
T entity = new T();
foreach (DataRow row in table.Rows)
{
foreach (var item in entity.GetType().GetProperties())
{
if (row.Table.Columns.Contains(item.Name))
{
if (DBNull.Value != row[item.Name])
{
Type newType = item.PropertyType;
//判断type类型是否为泛型,因为nullable是泛型类,
if (newType.IsGenericType
&& newType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类
{
//如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换
System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(newType);
//将type转换为nullable对的基础基元类型
newType = nullableConverter.UnderlyingType;
}
item.SetValue(entity, Convert.ChangeType(row[item.Name], newType), null);
}
}
}
}
return entity;
}
public static List<T> ToEntities<T>(this DataTable table) where T : new()
{
List<T> entities = new List<T>();
if (table == null)
return null;
foreach (DataRow row in table.Rows)
{
T entity = new T();
foreach (var item in entity.GetType().GetProperties())
{
if (table.Columns.Contains(item.Name))
{
if (DBNull.Value != row[item.Name])
{
Type newType = item.PropertyType;
//判断type类型是否为泛型,因为nullable是泛型类,
if (newType.IsGenericType
&& newType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类
{
//如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换
System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(newType);
//将type转换为nullable对的基础基元类型
newType = nullableConverter.UnderlyingType;
}
item.SetValue(entity, Convert.ChangeType(row[item.Name], newType), null);
}
}
}
entities.Add(entity);
}
return entities;
}
// public static dynamic ListToTranslate(this DataTable sender)
//{
// var result = new System.Collections.Generic.List();
// if (string.IsNullOrWhiteSpace(sender.Namespace) || string.IsNullOrWhiteSpace(sender.TableName))
// throw new Exception("Namespace or TableName is NullOrWhiteSpace");
// var typeStr = "";
// foreach (DataColumn cloumn in sender.Columns)
// {
// typeStr += " public string @2{set;get;}## ##".Replace("@2", cloumn.ColumnName);//Replace("@1", cloumn.DataType.Name).
// }
// typeStr = "namespace @1##{## public class @2## {##@3## }##}".Replace("@1", sender.Namespace).Replace("@2", sender.TableName).Replace("@3", typeStr).Replace("##", "\r\n");
// var cr = new CSharpCodeProvider().CompileAssemblyFromSource(new CompilerParameters(new string[] { "System.dll" }), typeStr);
// var type = cr.CompiledAssembly.GetType(string.Format("{0}.{1}", sender.Namespace, sender.TableName));
// var properties = type.GetProperties();
// foreach (DataRow row in sender.Rows)
// {
// var dm = Activator.CreateInstance(type);
// foreach (DataColumn cloumn in sender.Columns)
// {
// var property = properties.FirstOrDefault(l => IsEnter(l.Name, cloumn.ColumnName));
// if (property != null)
// {
// property.SetValue(dm, row[cloumn].ToString());
// }
// }
// result.Add(dm);
// }
// return result;
//}
}
}