C#DataTable(转List /JSON/字典 互转)【工具类】

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
#region  数据表DataTable 转键值对集合 List
   /// <summary>
   /// 数据表DataTable 转键值对集合 List
   /// 把DataTable转成 List集合, 存每一行
   /// 集合中放的是键值对字典,存每一列
   /// </summary>
   /// <param name="dt">数据表</param>
   /// <returns>哈希表数组</returns>
   public static List<Dictionary<string, object>> DataTableToList(DataTable dt)
   {
       List<Dictionary<string, object>> list= new List<Dictionary<string, object>>();
       foreach (DataRow dr in dt.Rows)
       {
           Dictionary<string, object> dic = new Dictionary<string, object>();
           foreach (DataColumn dc in dt.Columns)
           {
               dic.Add(dc.ColumnName, dr[dc.ColumnName]);
           }
           list.Add(dic);
       }
       return list;
   }
   #endregion
 
   #region 数据集转键值对数组字典
   /// <summary>
   /// 数据集转键值对数组字典
   /// </summary>
   /// <param name="dataSet">数据集</param>
   /// <returns>键值对数组字典</returns>
   public static Dictionary<string, List<Dictionary<string, object>>> DataSetToDic(DataSet ds)
   {
       Dictionary<string, List<Dictionary<string, object>>> result = new Dictionary<string, List<Dictionary<string, object>>>();
       foreach (DataTable dt in ds.Tables)
           result.Add(dt.TableName, DataTableToList(dt));
       return result;
   }
   #endregion
 
   #region 数据表Tables 转JSON
   /// <summary>
   /// 数据表转Tables JSON
   /// </summary>
   /// <param name="dataTable">数据表</param>
   /// <returns>JSON字符串</returns>
   public static string DataTableToJSON(DataTable dt)
   {
       return JsonHelper.ObjectToJSON(DataTableToList(dt));
   }
   #endregion
 
   #region 将datatable转换为json
   /// <summary>
   /// 将datatable转换为json
   /// </summary>
   /// <param name="dtb">Dt</param>
   /// <returns>JSON字符串</returns>
   public static string Dtb2Json(DataTable dtb)
   {
       System.Collections.ArrayList dic = new System.Collections.ArrayList();
       foreach (DataRow dr in dtb.Rows)
       {
           System.Collections.Generic.Dictionary<string, object> drow = new System.Collections.Generic.Dictionary<string, object>();
           foreach (DataColumn dc in dtb.Columns)
           {
               drow.Add(dc.ColumnName, dr[dc.ColumnName]);
           }
 
           dic.Add(drow);
       }
       return null;
   }
   #endregion
 
   #region 将Dictionary转换为数据表数据 Tables
   public static DataTable DictToDataTable(Dictionary<string, object> dict)
   {
       DataTable dt = new DataTable();
       foreach (var colName in dict.Keys)
       {
           dt.Columns.Add(colName, typeof(string));
       }
       DataRow dr = dt.NewRow();
       foreach (KeyValuePair<string, object> item in dict)
       {
           dr[item.Key] = item.Value;
       }
       dt.Rows.Add(dr);
       return dt;
   }
   #endregion
 
   #region 将List转换为数据表数据 Tables
   /// <summary>
   /// List转DataTable
   /// </summary>
   public static DataTable ListToDataTable<T>(List<T> list)
   {
       if (list == null || list.Count == 0)
       {
           return new DataTable();
       }
       //获取T下所有的属性
       Type entityType = list[0].GetType();
       PropertyInfo[] entityProperties = entityType.GetProperties();
       DataTable dt = new DataTable("data");
       for (int i = 0; i < entityProperties.Length; i++)
       {
           dt.Columns.Add(entityProperties[i].Name);
       }
       foreach (var item in list)
       {
           if (item.GetType() != entityType)
           {
               throw new Exception("要转换集合元素类型不一致!");
           }
           //创建一个用于放所有属性值的数组
           object[] entityValues = new object[entityProperties.Length];
           for (int i = 0; i < entityProperties.Length; i++)
           {
               entityValues[i] = entityProperties[i].GetValue(item, null);
           }
           dt.Rows.Add(entityValues);
       }
       return dt;
   }
   #endregion
 
   #region  Json 字符串 转换为 DataTable数据集合  简要版,正在使用中
   /// <summary>
   /// Json 字符串 转换为 DataTable数据集合  简要版,正在使用中
   /// </summary>
   /// <param name="json"></param>
   /// <returns></returns>
   ///
   //格式;
   //[{"mac":"20:f1:7c:c5:cd:80","rssi":"-86","ch":"9"},{"mac":"20:f1:7c:c5:cd:85","rssi":"-91","ch":"9"}]
   public static DataTable ToDataTableTwo(string json)
   {
       DataTable dataTable = new DataTable();  //实例化
       DataTable result;
       try
       {
           List<Dictionary<string, object>> arrayList = JsonHelper.JSONToObject<List<Dictionary<string, object>>>(json);
           if (arrayList != null && arrayList.Count > 0)
           {
               foreach (Dictionary<string, object> dictionary in arrayList)
               {
                   if (dictionary.Keys.Count == 0)
                   {
                       result = dataTable;
                       return result;
                   }
                   if (dataTable.Columns.Count == 0)
                   {
                       foreach (var current in dictionary)
                       {
                           if (current.Value != null)
                           {
                               dataTable.Columns.Add(current.Key, current.Value.GetType());
                           }
                           else
                           {
                               dataTable.Columns.Add(current.Key);
                           }
                       }
                   }
                   DataRow dataRow = dataTable.NewRow();
                   foreach (string current in dictionary.Keys)
                   {
                       if (dictionary[current] != null)
                       {
                           dataRow[current] = dictionary[current];
                       }
                   }
                   dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
               }
           }
       }
       catch (Exception ex)
       {
           throw ex;
       }
       result = dataTable;
       return result;
   }
   #endregion

  

posted @   hapgaoyi  阅读(229)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示