您好,欢迎访问一九零五行业门户网

C# DataTable 转换为 实体类对象实例

public class user { public int id { get; set; } public string name { get; set; } } //对应数据库表: //user //字段:id、name
那么你也许需要编写将datatable 转换为实体对象的方法,便利datatable.rows 获得并填充。。
下面是我写的一个通用方法,分享+记录,便于日后直接copy ~
private static list<t> tabletoentity<t>(datatable dt) where t : class,new() { type type = typeof(t); list<t> list = new list<t>(); foreach (datarow row in dt.rows) { propertyinfo[] parray = type.getproperties(); t entity = new t(); foreach (propertyinfo p in parray) { if (row[p.name] is int64) { p.setvalue(entity, convert.toint32(row[p.name]), null); continue; } p.setvalue(entity, row[p.name], null); } list.add(entity); } return list; } // 调用: list<user> userlist = tabletoentity<user>(yourdatatable);
更多c# datatable 转换为 实体类对象实例。
其它类似信息

推荐信息