DataTable转实体类集合

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);

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