DataTableHelp.cs
5.32 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.DataAnnotations;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Xml.Serialization;
namespace Hh.Mes.Common.DataTableTo
{
/// <summary>
/// DataTableHelp 的摘要说明
/// </summary>
public static class DataTableHelp
{
/// <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();
}
/// <summary>
/// 去重复数据方法
/// </summary>
public static DataTable Distinct(this DataTable dtSource, params string[] filedNames)
{
return dtSource.DefaultView.ToTable(true, filedNames);
}
/// <summary>
/// dt判断是否为null 或者 行数是否为0,true是为空,false存在数据
/// </summary>
public static bool IsEmpty(this DataTable dt)
{
return dt == null || dt.Rows.Count <= 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;
}
/// <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;
}
public static dynamic CalculateSumTime(this DataTable dtSource, string filterColumn, string sumColumn, dynamic filterValue)
{
// 使用 Select 方法查询指定列等于指定值的所有行
var rows = dtSource.AsEnumerable().Where(row => row.Field<dynamic>(filterColumn) == filterValue);
// 汇总指定列的值
dynamic totalSumTime = 0;
foreach (DataRow row in rows)
{
totalSumTime += row.Field<dynamic>(sumColumn);
}
return totalSumTime;
}
/// <summary>
/// 将DataTable的指定列转换为结构化字符串
/// </summary>
/// <param name="dataTable">数据源</param>
/// <param name="columnName">目标列名</param>
/// <param name="separator">分隔符(默认逗号)</param>
/// <param name="distinct">是否去重(默认否)</param>
public static string ConvertColumnToString(this DataTable dataTable, string columnName,
string separator = ",", bool distinct = true)
{
// 列存在性检查
if (!dataTable.Columns.Contains(columnName))
throw new ArgumentException($"列[{columnName}]不存在");
// 提取列值并处理空值
var query = dataTable.AsEnumerable()
.Select(row => row[columnName]?.ToString() ?? "")
.Cast<string>(); // 显式类型转换
// 去重逻辑
if (distinct)
query = (EnumerableRowCollection<string>)query.Distinct();
// 拼接为结构化字符串
return string.Join(separator, query);
}
}
}