forked from IoTSharp/IoTSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataExtension.cs
162 lines (154 loc) · 5.83 KB
/
DataExtension.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
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
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace System.Data
{
public static class DataExtension
{
public static IList<T> ToIList<T>(this DataTable dt) where T : class => dt.ToList<T>();
public static List<T> ToList<T>(this DataTable dt) where T : class
{
List<T> jArray = new List<T>();
var prs = typeof(T).GetProperties();
try
{
for (int il = 0; il < dt.Rows.Count; il++)
{
T jObject = Activator.CreateInstance<T>();
for (int i = 0; i < dt.Columns.Count; i++)
{
try
{
string strKey = dt.Columns[i].ColumnName;
if (dt.Rows[il].ItemArray[i] != DBNull.Value)
{
object obj = Convert.ChangeType(dt.Rows[il].ItemArray[i], dt.Columns[i].DataType);
var p = prs.FirstOrDefault(px => px.Name.ToLower() == strKey.ToLower());
if (p != null)
{
SetValue(jObject, dt.Columns[i].DataType, obj, p);
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
jArray.Add(jObject);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
return jArray;
}
/// <summary>
/// DataTable 转为 Json
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static JArray ToJson(this DataTable dt)
{
JArray jArray = new JArray();
try
{
for (int il = 0; il < dt.Rows.Count; il++)
{
JObject jObject = new JObject();
for (int i = 0; i < dt.Columns.Count; i++)
{
try
{
string strKey = dt.Columns[i].ColumnName;
if (dt.Rows[il].ItemArray[i] != DBNull.Value)
{
object obj = Convert.ChangeType(dt.Rows[il].ItemArray[i], dt.Columns[i].DataType);
jObject.Add(strKey, JToken.FromObject(obj));
}
}
catch (Exception)
{
}
}
jArray.Add(jObject);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
return jArray;
}
private static void SetValue<T>(T jObject, Type ft, object obj, System.Reflection.FieldInfo p)
{
if (p.FieldType == ft)
{
p.SetValue(jObject, obj);
}
else if (p.FieldType == typeof(DateTime) && ft == typeof(string))
{
if (DateTime.TryParse((string)obj, out DateTime dt))
{
p.SetValue(jObject, dt);
}
}
else
{
p.SetValue(jObject, Convert.ChangeType(obj, p.FieldType));
}
}
private static void SetValue<T>(T jObject, Type ft, object obj, System.Reflection.PropertyInfo p) where T : class
{
if (p.PropertyType == ft)
{
p.SetValue(jObject, obj);
}
else if (p.PropertyType == typeof(DateTime) && ft == typeof(string))
{
if (DateTime.TryParse((string)obj, out DateTime dt))
{
p.SetValue(jObject, dt);
}
}
else
{
p.SetValue(jObject, Convert.ChangeType(obj, p.PropertyType));
}
}
/// <summary>
///将DataTable转换为标准的CSV字符串
/// </summary>
/// <param name="dt">数据表</param>
/// <returns>返回标准的CSV</returns>
/// <seealso cref="https://github.com/Coldairarrow/EFCore.Sharding/blob/5216ffc6330e84de484865eae645c0be1f2be180/src/EFCore.Sharding.Tests/Util/Extention.DataTable.cs"/>
public static string ToCsvStr(this DataTable dt)
{
//以半角逗号(即,)作分隔符,列为空也要表达其存在。
//列内容如存在半角逗号(即,)则用半角引号(即"")将该字段值包含起来。
//列内容如存在半角引号(即")则应替换成半角双引号("")转义,并用半角引号(即"")将该字段值包含起来。
StringBuilder sb = new StringBuilder();
DataColumn colum;
foreach (DataRow row in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
colum = dt.Columns[i];
if (i != 0) sb.Append(",");
if (colum.DataType == typeof(string) && row[colum].ToString().Contains(","))
{
sb.Append("\"" + row[colum].ToString().Replace("\"", "\"\"") + "\"");
}
else sb.Append(row[colum].ToString());
}
sb.AppendLine();
}
return sb.ToString();
}
}
}