DtToList.cs
5.08 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace HHECS.Infrastructure.CommonHelper
{
public class DtToList
{
/// <summary>
/// 根据实体类创建表结构
/// </summary>
/// <param name="model">实体类</param>
/// <returns></returns>
public static DataTable CreateDataTable<T>(T model, List<string> cols)
{
DataTable dataTable = new DataTable(typeof(T).Name);
foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
{
try
{
if (cols.Contains(propertyInfo.Name))
{
dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));
}
}
catch (Exception ex)
{
dataTable.Columns.Add(new DataColumn(propertyInfo.Name, typeof(string)));
}
}
return dataTable;
}
/// <summary>
/// List转换成DataTable
/// </summary>
/// <param name="modelList">List集合</param>
/// <returns></returns>
public static DataTable ListConvertToDataTable<T>(List<T> modelList, List<string> cols)
{
if (modelList == null || modelList.Count == 0)
{
return null;
}
DataTable dt = CreateDataTable(modelList[0], cols);
foreach (T model in modelList)
{
DataRow dataRow = dt.NewRow();
foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
{
try
{
if (cols.Contains(propertyInfo.Name))
{
dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
}
}
catch (Exception)
{
}
}
dt.Rows.Add(dataRow);
}
return dt;
}
/// <summary>
/// datatable转list
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> ConvertToModel<T>(DataTable dt) where T : new()
{
List<T> ts = new List<T>();// 定义集合
string tempName = "";
foreach (DataRow dr in dt.Rows)
{
T t = new T();
PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name;
if (dt.Columns.Contains(tempName))
{
if (!pi.CanWrite) continue;
object value = dr[tempName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
ts.Add(t);
}
return ts;
}
/// <summary>
/// list转datatable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection"></param>
/// <returns></returns>
public static DataTable ListToDt<T>(IEnumerable<T> collection)
{
var props = typeof(T).GetProperties().Where(t => t.GetCustomAttribute(typeof(EditableAttribute)) == null);
var dt = new DataTable();
foreach (var item in props)
{
DataColumn dataColumn;
Type colType = item.PropertyType;
if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
colType = colType.GetGenericArguments()[0];
}
var attr = (DescriptionAttribute)item.GetCustomAttribute(typeof(DescriptionAttribute));
if (attr != null)
{
dataColumn = new DataColumn(attr.Description, colType);
}
else
{
dataColumn = new DataColumn(item.Name, colType);
}
dt.Columns.Add(dataColumn);
}
if (collection.Count() > 0)
{
for (int i = 0; i < collection.Count(); i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in props)
{
object obj = pi.GetValue(collection.ElementAt(i), null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
dt.LoadDataRow(array, true);
}
}
return dt;
}
}
}