DataTableHelp.cs
11.4 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Xml.Serialization;
using System.Text.RegularExpressions;
using System.Linq.Expressions;
namespace Hh.Mes.Common.DataTableTo
{
/// <summary>
/// DataTableHelp 的摘要说明
/// </summary>
public static class DataTableHelp
{
#region 获取某一列的所有值(字符串拼接手动 string.Join(",", x))
/// <summary>
/// 获取某一列的所有值 (字符串拼接手动 string.Join(",", x))
/// </summary>
/// <typeparam name="T">列数据类型</typeparam>
/// <param name="dtSource">数据表</param>
/// <param name="filedName">列名</param>
/// <returns></returns>
public static List<T> GetColumnValues<T>(this DataTable dtSource, string filedName)
{
return (from r in dtSource.AsEnumerable() select r.Field<T>(filedName)).ToList();
}
#endregion
#region 去重复数据方法
/// <summary>
/// 去重复数据方法
/// </summary>
public static DataTable Distinct(this DataTable dtSource, params string[] filedNames)
{
return dtSource.DefaultView.ToTable(true, filedNames);
}
#endregion
#region dt判断是否为null 或者 行数是否为0,true是为空,false存在数据
/// <summary>
/// dt判断是否为null 或者 行数是否为0,true是为空,false存在数据
/// </summary>
public static bool IsEmpty(this DataTable dt)
{
return dt == null || dt.Rows.Count <= 0;
}
#endregion
#region string 空返回0,
/// <summary>
/// string 空返回0,
/// </summary>
public static string IsEmpty(this object val, string retVal = "")
{
if (val == null) return "";
var temp = val.ToString();
return string.IsNullOrEmpty(temp) ? retVal : temp;
}
#endregion
#region list转datatable DataTableHelp.ListToDt(list)
/// <summary>
/// list转datatable DataTableHelp.ListToDt(list)
/// </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;
}
#endregion
#region DataTable转List
#region 构建自定义特性(用于获取属性的对应的表格列名)
/// <summary>
/// 构建自定义特性(用于获取属性的对应的表格列名)
/// </summary>
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public class ExcelColumnAttribute : Attribute
{
public string Name { get; }
public ExcelColumnAttribute(string name)
{
Name = name;
}
}
#endregion
private static readonly Dictionary<Type, object> _mappingCache = new Dictionary<Type, object>();
private static readonly Regex _lineBreakRegex = new Regex(@"[\r\n]+", RegexOptions.Compiled);
/// <summary>
/// DataTable转为List<Model>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataTable"></param>
/// <returns></returns>
public static List<T> ToList<T>(this DataTable dataTable) where T : new()
{
//先重构表
DataTable newTable = RefactoringTables(dataTable);
var mappings = GetMappings<T>(newTable);
var result = new List<T>(newTable.Rows.Count);
var lst_dtColumnNames = newTable.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToList();
foreach (DataRow row in newTable.Rows)
{
var obj = new T();
foreach (var mapping in mappings)
{
//object value = GetRowValue(row, mapping, newTable);
object value = GetRowValue(row, mapping, lst_dtColumnNames);
mapping.Setter(obj, value);
}
result.Add(obj);
}
return result;
}
#region 重构表格(大数据的情况下效率不高)
/// <summary>
/// 重构表格
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
private static DataTable RefactoringTables(DataTable dt)
{
DataTable newTable = dt.Copy();
foreach (DataColumn col in newTable.Columns)
{
col.ColumnName = _lineBreakRegex.Replace(col.ColumnName, "");
}
return newTable;
}
#endregion
#region 获取表格数值
/// <summary>
/// 获取值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="row"></param>
/// <param name="mapping"></param>
/// <param name="dt"></param>
/// <returns></returns>
//private static object GetRowValue<T>(DataRow row, PropertyMapping<T> mapping, DataTable dt)
private static object GetRowValue<T>(DataRow row, PropertyMapping<T> mapping, List<string> lst_dtColumnNames)
{
//检查列名是否存在
//bool columnExists = dt.Columns.Contains(mapping.ColumnName);
bool columnExists = lst_dtColumnNames.Contains(mapping.ColumnName);
object value = columnExists ? row[mapping.ColumnName] : null;
//处理DBNull和未找到列的情况
if (value == DBNull.Value || !columnExists)
{
return mapping.IsStringType ? "" : mapping.DefaultValue;
}
try
{
return Convert.ChangeType(value, mapping.PropertyType);
}
catch
{
return mapping.DefaultValue;
}
}
#endregion
#region 转成对应Model的Mapping
/// <summary>
/// 转成对应Model的Mapping
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataTable"></param>
/// <returns></returns>
private static PropertyMapping<T>[] GetMappings<T>(DataTable dataTable) where T : new()
{
var type = typeof(T);
if (_mappingCache.TryGetValue(type, out var cachedMappings))
return (PropertyMapping<T>[])cachedMappings;
var properties = type.GetProperties()
.Where(p => p.CanWrite)
.Select(p => new PropertyMapping<T>
{
Property = p,
ColumnName = GetColumnName(p, dataTable),
PropertyType = p.PropertyType,
IsStringType = p.PropertyType == typeof(string),
DefaultValue = p.PropertyType.IsValueType ?
Activator.CreateInstance(p.PropertyType) : null,
Setter = CreateSetter<T>(p)
})
.ToArray();
_mappingCache[type] = properties;
return properties;
}
#endregion
#region obj类型转换
/// <summary>
/// obj类型转换
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="property"></param>
/// <returns></returns>
private static Action<T, object> CreateSetter<T>(PropertyInfo property)
{
var instance = Expression.Parameter(typeof(T), "obj");
var value = Expression.Parameter(typeof(object), "value");
// 转换object到实际类型
var convertExpr = Expression.Convert(
Expression.Convert(value, property.PropertyType),
property.PropertyType);
var setterCall = Expression.Call(
instance,
property.GetSetMethod(),
convertExpr);
return Expression.Lambda<Action<T, object>>(setterCall, instance, value)
.Compile();
}
#endregion
#region 获取匹配列名(待新增读取配置文件方法)
/// <summary>
/// 获取匹配列名(待新增读取配置文件方法)
/// </summary>
/// <param name="property"></param>
/// <param name="dataTable"></param>
/// <returns></returns>
private static string GetColumnName(PropertyInfo property, DataTable dataTable)
{
//如新增读取配置文件方法可以增加标志位或者设置优先级
//优先读取ColumnAttribute
var excelColumnAttr = property.GetCustomAttribute<ExcelColumnAttribute>();
if (!string.IsNullOrEmpty(excelColumnAttr?.Name))
{
return excelColumnAttr.Name;
}
//匹配列名(不区分大小写)
var columnNames = dataTable.Columns
.Cast<DataColumn>()
.Select(c => c.ColumnName)
.ToList();
return columnNames.FirstOrDefault(c =>
c.Equals(property.Name, StringComparison.OrdinalIgnoreCase)) ?? property.Name;
}
#endregion
#region 构建Mapping
/// <summary>
/// 构建Mapping
/// </summary>
/// <typeparam name="T"></typeparam>
private class PropertyMapping<T>
{
public PropertyInfo Property { get; set; }
public string ColumnName { get; set; }
public Type PropertyType { get; set; }
public bool IsStringType { get; set; }
public object DefaultValue { get; set; }
public Action<T, object> Setter { get; set; }
}
#endregion
#endregion
}
}