C# DataTable to List based on reflection.
程序员文章站
2022-05-18 15:33:21
From https://www.cnblogs.com/zjbky/p/9242140.html ......
from
static class extendclass { public static list<t> todatalist<t>(this datatable dt) { var list = new list<t>(); var plist = new list<propertyinfo>(typeof(t).getproperties()); foreach (datarow item in dt.rows) { t s = activator.createinstance<t>(); for (int i = 0; i < dt.columns.count; i++) { propertyinfo info = plist.find(p => p.name == dt.columns[i].columnname); if (info != null) { try { if (!convert.isdbnull(item[i])) { object v = null; if (info.propertytype.tostring().contains("system.nullable")) { v = convert.changetype(item[i], nullable.getunderlyingtype(info.propertytype)); } else { v = convert.changetype(item[i], info.propertytype); } info.setvalue(s, v, null); } } catch (exception ex) { throw new exception("字段[" + info.name + "]转换出错," + ex.message); } } } list.add(s); } return list; } }
上一篇: 你是什么垃圾?
下一篇: Hadoop介绍与安装
推荐阅读
-
C# convert json to datatable,convert list to datatable
-
C#实现DataTable,List和Json转换的方法
-
C# DataTable to List
based on reflection. -
C#常见的几种集合 ArrayList,Hashtable,List
,Dictionary 遍历方法对比 -
C# 把List 集合转换成DataTable
-
C# 中的集合(Array/ArrayList/List
/HashTable/Dictionary) -
C# 中数组、ArrayList、List
区别 -
C# 两个List
(T是类)使用Contains比较元素的方法 -
C# 中使用Linq和Lambda表达式对List
进行排序 -
DataTable与List
相互转换