首页 > 其他 > 详细

DataTable转实体类集合

时间:2018-01-22 10:20:24      阅读:305      评论:0      收藏:0      [点我收藏+]

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].ToString(), null);
}
list.Add(entity);
}
return list;
}

调用:List<AlarmMachinelist> userList = TableToEntity<AlarmMachinelist>(dt);

DataTable转实体类集合

原文:https://www.cnblogs.com/wuyujie/p/8327615.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!