C# List<T> 转 DataTable 方法修改版
之前写了个 List<T> item.toDataTable() 这种链式调用的List<T>转换为DataTable的方法,有两位热心的网友提出了存在的一些缺陷,如果传入的obj参数不是List而是单个object对象或传入的是单类型的List时转换会出错,现对代码进行了些修改,反正代码测试基本是跑通了,对List<T>,单个Object对象,以及List<string>、List<int>这类传入的数据都能转,但还是感觉整个判断过程可能不够严谨,希望高手指正。
之前的代码:C# List<T> 转 DataTable
public static DataTable toDataTable(this object obj,string _tName = null) { Type t = obj.GetType(); dynamic ts = obj; DataTable DT = new DataTable(); PropertyInfo[] pi; //判断obj是否为单个的实体类 if (t.Name.IndexOf("List") < 0) { pi = ts.GetType().GetProperties(BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); DT.TableName = _tName == null ? ts.GetType().ToString() : _tName; foreach (PropertyInfo p in pi) { DT.Columns.Add(p.Name, p.PropertyType); } DataRow dr1 = DT.NewRow(); foreach (PropertyInfo p in pi) { dr1[p.Name] = p.GetValue(ts, null); } DT.Rows.Add(dr1); return DT; } //判断obj是否为单类型对象 if (t.ToString().IndexOf("System.String")>=0 || t.ToString().IndexOf("System.Object")>=0 || t.ToString().IndexOf("System.Int")>=0 || t.ToString().IndexOf("System.Boolean")>=0) { DT.TableName = _tName == null ? "Table1" : _tName; DT.Columns.Add("Column1", typeof(object)); foreach (var v in ts) { DataRow dr2 = DT.NewRow(); dr2["Column1"] = v ; DT.Rows.Add(dr2); } return DT; } pi = ts[0].GetType().GetProperties(BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); DT.TableName = _tName == null ? ts[0].GetType().ToString() : _tName; foreach (PropertyInfo p in pi) { DT.Columns.Add(p.Name, p.PropertyType); } DataRow dr = null; foreach (var v in ts) { dr = DT.NewRow(); foreach (PropertyInfo p in pi) { dr[p.Name] = p.GetValue(v, null); } DT.Rows.Add(dr); } return DT; }
测试代码:
private static void ReadDataTableFromList() { ConnConfig conn = new ConnConfig(dbtype: DBType.SQLServer, connstring: SQLConnStr); DBHelper<VMIS_model> vioDB = new DBHelper<VMIS_model>(conn); List<VMIS_model> list = vioDB.Select(whereSQL: "PlateNumber=@PN", sqlParams: new ORMParameter[] { new ORMParameter("@PN","车牌ABCDE") }); Console.WriteLine("List<T>.toDataTable() 测试:"); DataTable DT = list.toDataTable("List_VMIS"); Console.WriteLine("数据加载到DataTable 【" + DT.TableName + "】 完成,共载入数据:" + DT.Rows.Count.ToString()); Console.WriteLine(JsonConvert.SerializeObject(DT, Formatting.Indented)); Console.WriteLine("----------------------------------------------------------------------"); Console.WriteLine("单个object.toDataTable() 测试:"); VMIS_model vi = list.First(); DT = vi.toDataTable("VMIS"); Console.WriteLine("数据加载到DataTable 【" + DT.TableName + "】 完成,共载入数据:" + DT.Rows.Count.ToString()); Console.WriteLine(JsonConvert.SerializeObject(DT, Formatting.Indented)); Console.WriteLine("----------------------------------------------------------------------"); Console.WriteLine("List<string>.toDataTable() 测试:"); List<string> list1 = new List<string>(); list1.Add("张三"); list1.Add("李四"); list1.Add("王五"); DT = list1.toDataTable("List_String"); Console.WriteLine("数据加载到DataTable 【" + DT.TableName + "】 完成,共载入数据:" + DT.Rows.Count.ToString()); Console.WriteLine(JsonConvert.SerializeObject(DT, Formatting.Indented)); Console.WriteLine("----------------------------------------------------------------------"); Console.WriteLine("List<int>.toDataTable() 测试:"); List<int> list2 = new List<int>(); list2.Add(-2021); list2.Add(2022); list2.Add(2023); DT = list2.toDataTable("List_Int"); Console.WriteLine("数据加载到DataTable 【" + DT.TableName + "】 完成,共载入数据:" + DT.Rows.Count.ToString()); Console.WriteLine(JsonConvert.SerializeObject(DT, Formatting.Indented)); Console.WriteLine("----------------------------------------------------------------------"); Console.WriteLine("List<long>.toDataTable() 测试:"); List<long> list3 = new List<long>(); list3.Add(3232235876000); list3.Add(3232235877000); list3.Add(-3232235878000); DT = list3.toDataTable(); Console.WriteLine("数据加载到DataTable 【" + DT.TableName + "】 完成,共载入数据:" + DT.Rows.Count.ToString()); Console.WriteLine(JsonConvert.SerializeObject(DT, Formatting.Indented)); Console.WriteLine("----------------------------------------------------------------------"); Console.WriteLine("List<bool>.toDataTable() 测试:"); List<bool> list4 = new List<bool>(); list4.Add(true); list4.Add(false); DT = list4.toDataTable("List_Bool"); Console.WriteLine("数据加载到DataTable 【" + DT.TableName + "】 完成,共载入数据:" + DT.Rows.Count.ToString()); Console.WriteLine(JsonConvert.SerializeObject(DT, Formatting.Indented)); Console.WriteLine("----------------------------------------------------------------------"); Console.WriteLine("List<object>.toDataTable() 测试:"); List<object> list5 = new List<object>(); list5.Add("张三"); list5.Add(2022); list5.Add(-2023); list5.Add(3232235877000); list5.Add(-3232235878000); list5.Add(true); list5.Add(false); DT = list5.toDataTable("List_Object"); Console.WriteLine("数据加载到DataTable 【" + DT.TableName + "】 完成,共载入数据:" + DT.Rows.Count.ToString()); Console.WriteLine(JsonConvert.SerializeObject(DT, Formatting.Indented)); Console.WriteLine("----------------------------------------------------------------------"); }
结果:
List<T> to DataTable 转换测试: --------------------------------------------- List<T>.toDataTable() 测试: 数据加载到DataTable 【List_VMIS】 完成,共载入数据:3 [ { "ID": "2105082103590123269484848995", "PlateNumber": "车牌ABCDE", "V_Text": "超速,超过限速110.00%", "V_Address": "超速卡口:xxxxx", "V_date": "2021-05-08T21:03:43", "CarOwner": "" }, { "ID": "2105082103590435798531004957", "PlateNumber": "车牌ABCDE", "V_Text": "超速,超过限速113.33%", "V_Address": "超速卡口:xxxxx", "V_date": "2021-05-08T21:03:13", "CarOwner": "" }, { "ID": "2105082147592512464100535248", "PlateNumber": "车牌ABCDE", "V_Text": "超速,超过限速83.33%", "V_Address": "超速卡口:xxxxx", "V_date": "2021-05-08T21:47:50", "CarOwner": "" } ] ---------------------------------------------------------------------- 单个object.toDataTable() 测试: 数据加载到DataTable 【VMIS】 完成,共载入数据:1 [ { "ID": "2105082103590123269484848995", "PlateNumber": "车牌ABCDE", "V_Text": "超速,超过限速110.00%", "V_Address": "超速卡口:xxxxx", "V_date": "2021-05-08T21:03:43", "CarOwner": "" } ] ---------------------------------------------------------------------- List<string>.toDataTable() 测试: 数据加载到DataTable 【List_String】 完成,共载入数据:3 [ { "Column1": "张三" }, { "Column1": "李四" }, { "Column1": "王五" } ] ---------------------------------------------------------------------- List<int>.toDataTable() 测试: 数据加载到DataTable 【List_Int】 完成,共载入数据:3 [ { "Column1": -2021 }, { "Column1": 2022 }, { "Column1": 2023 } ] ---------------------------------------------------------------------- List<long>.toDataTable() 测试: 数据加载到DataTable 【Table1】 完成,共载入数据:3 [ { "Column1": 3232235876000 }, { "Column1": 3232235877000 }, { "Column1": -3232235878000 } ] ---------------------------------------------------------------------- List<bool>.toDataTable() 测试: 数据加载到DataTable 【List_Bool】 完成,共载入数据:2 [ { "Column1": true }, { "Column1": false } ] ---------------------------------------------------------------------- List<object>.toDataTable() 测试: 数据加载到DataTable 【List_Object】 完成,共载入数据:7 [ { "Column1": "张三" }, { "Column1": 2022 }, { "Column1": -2023 }, { "Column1": 3232235877000 }, { "Column1": -3232235878000 }, { "Column1": true }, { "Column1": false } ] ----------------------------------------------------------------------
C# List<T> 转 DataTable - 小政哥01 - 博客园 (cnblogs.com)