
一、DataTable & List 相互转换
1.DataTable 转换为 List
| using System.Data; |
| using System.Reflection; |
| |
| |
| |
| |
| |
| |
| public static List<T> DataTableToList<T>(DataTable dataTable) where T : class, new() |
| { |
| |
| List<T> ts = new List<T>(); |
| |
| string tempName = string.Empty; |
| |
| foreach (DataRow dr in dataTable.Rows) |
| { |
| T t = new T(); |
| |
| PropertyInfo[] propertys = t.GetType().GetProperties(); |
| |
| foreach (System.Reflection.PropertyInfo pi in propertys) |
| { |
| tempName = pi.Name; |
| |
| if (dataTable.Columns.Contains(tempName)) |
| { |
| |
| object value = dr[tempName]; |
| |
| if (value != DBNull.Value) |
| { |
| pi.SetValue(t, value, null); |
| } |
| } |
| } |
| |
| ts.Add(t); |
| } |
| return ts; |
| } |
2.List 转换为 DataTable
需要引用Newtonsoft.Json包
| using Newtonsoft.Json; |
| using System.Data; |
| |
| |
| |
| |
| |
| |
| public static DataTable ListToDataTable<T>(List<T> list) |
| { |
| |
| DataTable dt = new DataTable("tableName"); |
| |
| |
| foreach (var item in list.FirstOrDefault().GetType().GetProperties()) |
| { |
| dt.Columns.Add(item.Name); |
| } |
| |
| foreach (var item in list) |
| { |
| |
| DataRow value = dt.NewRow(); |
| |
| foreach (DataColumn dtColumn in dt.Columns) |
| { |
| int i = dt.Columns.IndexOf(dtColumn); |
| |
| if (value.GetType().IsPrimitive) |
| { |
| value[i] = item.GetType().GetProperty(dtColumn.ColumnName).GetValue(item); |
| } |
| else |
| { |
| value[i] = JsonConvert.SerializeObject(item.GetType().GetProperty(dtColumn.ColumnName).GetValue(item)); |
| } |
| } |
| dt.Rows.Add(value); |
| } |
| |
| return dt; |
| } |
二、DataTable & Json 相互转换
1.DataTable 转换为 Json字符串
需要引用Newtonsoft.Json包
| using Newtonsoft.Json; |
| using System.Collections; |
| using System.Data; |
| |
| |
| |
| |
| |
| |
| public static string DataTableToJson(DataTable dataTable) |
| { |
| |
| ArrayList arrayList = new ArrayList(); |
| |
| foreach (DataRow dr in dataTable.Rows) |
| { |
| |
| Dictionary<string, object> columnsDic = new Dictionary<string, object>(); |
| |
| foreach (DataColumn dc in dataTable.Columns) |
| { |
| |
| columnsDic.Add(dc.ColumnName, dr[dc.ColumnName]); |
| } |
| |
| arrayList.Add(columnsDic); |
| } |
| |
| return JsonConvert.SerializeObject(arrayList); |
| } |
2.Json 转换为 DataTable
| using System.Data; |
| using System.Text.RegularExpressions; |
| |
| |
| |
| |
| |
| |
| public static DataTable JsonToDataTable(string strJson) |
| { |
| |
| strJson = strJson.Replace(",\"", "*\"").Replace("\":", "\"#").ToString(); |
| |
| var rg = new Regex(@"(?<={)[^:]+(?=:\[)", RegexOptions.IgnoreCase); |
| string strName = rg.Match(strJson).Value; |
| DataTable tb = null; |
| |
| strJson = strJson.Substring(strJson.IndexOf("[") + 1); |
| strJson = strJson.Substring(0, strJson.IndexOf("]")); |
| |
| |
| rg = new Regex(@"(?<={)[^}]+(?=})"); |
| MatchCollection mc = rg.Matches(strJson); |
| for (int i = 0; i < mc.Count; i++) |
| { |
| string strRow = mc[i].Value; |
| string[] strRows = strRow.Split('*'); |
| |
| |
| if (tb == null) |
| { |
| tb = new DataTable(); |
| tb.TableName = strName; |
| foreach (string str in strRows) |
| { |
| var dc = new DataColumn(); |
| string[] strCell = str.Split('#'); |
| |
| if (strCell[0].Substring(0, 1) == "\"") |
| { |
| int a = strCell[0].Length; |
| dc.ColumnName = strCell[0].Substring(1, a - 2); |
| } |
| else |
| { |
| dc.ColumnName = strCell[0]; |
| } |
| tb.Columns.Add(dc); |
| } |
| tb.AcceptChanges(); |
| } |
| |
| |
| DataRow dr = tb.NewRow(); |
| for (int r = 0; r < strRows.Length; r++) |
| { |
| dr[r] = strRows[r].Split('#')[1].Trim().Replace(",", ",").Replace(":", ":").Replace("\"", ""); |
| } |
| tb.Rows.Add(dr); |
| tb.AcceptChanges(); |
| } |
| |
| return tb; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?