jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using System.Collections;
using System.Web.Script.Serialization;
 
namespace HH.WMS.Utils
{
    public partial class ZConvert
    {
        /// <summary>
        /// 转换 DataTable 对象为 IList 对象
        /// </summary>
        /// <param name="datas">数据集合</param>
        /// <returns>数组对象</returns>
        public static T[] ToArray<T>(DataTable datas) where T : class, new()
        {
            List<T> list = ToList<T>(datas) as List<T>;
            return list.ToArray();
        }
 
        /// <summary>
        /// 转换IList对象为DataTable对象
        /// </summary>
        /// <param name="datas">数据集合</param>
        /// <returns>DataTable对象</returns>
        public static DataTable ToDataTable<T>(IList<T> datas)
        {
            return ToDataTable<T>(datas, null);
        }
 
        /// <summary>
        /// 转换IList对象为DataTable对象
        /// </summary>
        /// <param name="datas">数据集合</param>
        /// <returns>DataTable对象</returns>
        public static DataTable ToDataTable<T>(T[] datas)
        {
            return ToDataTable<T>(datas, null);
        }
 
        /// <summary>
        /// 转换IList对象为DataTable对象
        /// </summary>
        /// <param name="datas">数据集合</param>
        /// <param name="tableName">要创建的表名</param>
        /// <returns>DataTable对象</returns>
        public static DataTable ToDataTable<T>(IList<T> datas, string tableName)
        {
            Type type = typeof(T);
            if (string.IsNullOrEmpty(tableName))
            {
                tableName = type.Name;
            }
            DataTable table = new DataTable(tableName);
            table.BeginLoadData();
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo info in properties)
            {
                string typeName = info.PropertyType.ToString();
                if (info.PropertyType.IsGenericType)
                {
                    typeName = info.PropertyType.GetGenericArguments()[0].ToString();
                }
                Type type2 = Type.GetType(typeName, false);
                if (type2 != null)
                {
                    table.Columns.Add(info.Name, type2);
                }
            }
            if ((datas != null) && (datas.Count > 0))
            {
                foreach (object obj2 in datas)
                {
                    DataRow row = table.NewRow();
                    foreach (PropertyInfo info2 in properties)
                    {
                        if ((Type.GetType(info2.PropertyType.ToString(), false) != null) && (info2.GetValue(obj2, null) != null))
                        {
                            row[info2.Name] = info2.GetValue(obj2, null);
                        }
                    }
                    table.Rows.Add(row);
                }
            }
            table.EndLoadData();
            table.AcceptChanges();
            return table;
        }
 
        public static DataTable ListToDataTable(object datas, string tableName)
        {
            Type type = ZGeneric.GetGenericType(datas);
            if (string.IsNullOrEmpty(tableName))
                tableName = type.Name;
            
            DataTable table = new DataTable(tableName);
            table.BeginLoadData();
 
            var properties = ZReflection.GetProperties(type);
            foreach (var p in properties)
            {
                Type colType = p.Value.PropertyType;
                string typeName = colType.ToString();
                if (colType.IsGenericType)
                    typeName = colType.GetGenericArguments()[0].ToString();
                
                Type newType = Type.GetType(typeName, false);
                if (newType != null)
                    table.Columns.Add(p.Value.Name, newType);
            }
 
            IEnumerator enumerator = ((dynamic)datas).GetEnumerator();
            while (enumerator.MoveNext())
            {
                DataRow row = table.NewRow();
                foreach (var p in properties)
                {
                    var value = ZGeneric.GetValue(enumerator.Current, p.Value.Name);
                    if ((Type.GetType(p.Value.PropertyType.ToString(), false) != null) && (value != null))
                        row[p.Value.Name] = value;
                }
                table.Rows.Add(row);
            }
            table.EndLoadData();
            table.AcceptChanges();
            return table;
        }
 
        /// <summary>
        /// 转换IList对象为DataTable对象
        /// </summary>
        /// <param name="datas">数据集合</param>
        /// <param name="tableName">要创建的表名</param>
        /// <returns>DataTable对象</returns>
        public static DataTable ToDataTable<T>(T[] datas, string tableName)
        {
            IList<T> list;
            if ((datas == null) || (datas.Length == 0))
            {
                list = new List<T>();
            }
            else
            {
                list = new List<T>(datas);
            }
            return ToDataTable<T>(list, tableName);
        }
 
        /// <summary>
        /// 转换 DataTable 对象为 IList 对象
        /// </summary>
        /// <param name="datas">数据集合</param>
        /// <returns>IList 对象</returns>
        public static IList<T> ToList<T>(DataTable datas) where T : class, new()
        {
            IList<T> list = new List<T>();
            if ((datas != null) && (datas.Rows.Count != 0))
            {
                PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
                foreach (DataRow row in datas.Rows)
                {
                    T local = Activator.CreateInstance<T>();
                    foreach (DataColumn column in datas.Columns)
                    {
                        object obj2 = null;
                        if (row.RowState == DataRowState.Deleted)
                        {
                            obj2 = row[column, DataRowVersion.Original];
                        }
                        else
                        {
                            obj2 = row[column];
                        }
                        if (obj2 != DBNull.Value)
                        {
                            foreach (PropertyInfo info in properties)
                            {
                                if (column.ColumnName.Equals(info.Name, StringComparison.CurrentCultureIgnoreCase))
                                {
                                    info.SetValue(local, obj2, null);
                                }
                            }
                        }
                    }
                    list.Add(local);
                }
            }
            return list;
        }
 
        /// <summary>
        /// Json 字符串 转换为 DataTable数据集合
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static DataTable ToDataTableTwo(string json)
        {
            DataTable dataTable = new DataTable();  //实例化
            DataTable result;
            try
            {
                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
                ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
                if (arrayList.Count > 0)
                {
                    foreach (Dictionary<string, object> dictionary in arrayList)
                    {
                        if (dictionary.Keys.Count<string>() == 0)
                        {
                            result = dataTable;
                            return result;
                        }
                        //Columns
                        if (dataTable.Columns.Count == 0)
                        {
                            foreach (string current in dictionary.Keys)
                            {
                                dataTable.Columns.Add(current, dictionary[current]!=null?dictionary[current].GetType():typeof(string));
                            }
                        }
                        //Rows
                        DataRow dataRow = dataTable.NewRow();
                        foreach (string current in dictionary.Keys)
                        {
                            dataRow[current] = dictionary[current];
                        }
                        dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
                    }
                }
            }
            catch
            {
            }
            result = dataTable;
            return result;
        }
 
    }
}