备注:摘自网上 有附地址

public static List<T> DataTableToEntities<T>(this DataTable dt) where T : class, new()
{
if (null == dt || dt.Rows.Count == ) { return null; }
List<T> entities = new List<T>();
List<string> columnNames = new List<string>(); for (int i = ; i < dt.Columns.Count; i++)
{
columnNames.Add(dt.Columns[i].ColumnName);
} foreach (DataRow row in dt.Rows)
{
PropertyInfo[] pArray = typeof(T).GetProperties();
T entity = new T(); Array.ForEach<PropertyInfo>(pArray, p =>
{
if (!columnNames.Contains(p.Name))
{
return;
} object cellvalue = row[p.Name]; //空值不处理
if (cellvalue == DBNull.Value)
{
return;
}
if ((cellvalue == null) || string.IsNullOrWhiteSpace(cellvalue.ToString().Trim()))
{
return;
} if (cellvalue != DBNull.Value)
{
//经过了几个版本的迭代,最后一个为最新的,摘自网上,已附原文地址 //4、原地址:https://blog.csdn.net/Simon1003/article/details/80839744
if (!p.PropertyType.IsGenericType)
{
p.SetValue(entity, Convert.ChangeType(cellvalue, p.PropertyType), null);
}
else
{
Type genericTypeDefinition = p.PropertyType.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(Nullable<>))
{
p.SetValue(entity, Convert.ChangeType(cellvalue, Nullable.GetUnderlyingType(p.PropertyType)), null);
}
else
{
throw new Exception("genericTypeDefinition != typeof(Nullable<>)");
}
} //3、原地址:https://blog.csdn.net/hebbers/article/details/78957569
//Type type = p.PropertyType;
//if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类
//{
// //如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换
// System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(type);
// //将type转换为nullable对的基础基元类型
// type = nullableConverter.UnderlyingType;
//}
//p.SetValue(entity, Convert.ChangeType(cellvalue, type), null); //2、自定义 这种很傻,但当前解决速度最快
//if (p.PropertyType.Name.Equals("Int32"))
//{
// p.SetValue(entity, Convert.ToInt32(value), null);
//}
//else if (p.PropertyType.Name.Equals("String"))
//{
// p.SetValue(entity, Convert.ToString(value), null);
//}
//else if (p.PropertyType.Name.Equals("Nullable`1"))
//{
// p.SetValue(entity, Convert.ToInt32(value), null);
//}
////其它类型 暂时不管 //1、字段不为空可以用这种
//p.SetValue(entity, value, null);
}
});
entities.Add(entity);
}
return entities;
}

最新文章

  1. NPOI导出Excel
  2. GO语言面向对象
  3. C++中随机数和不重复的随机数
  4. Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules的解决办法
  5. Android 内容提供者简介
  6. 第三十一课:JSDeferred详解2
  7. [Twisted] Test
  8. Qt 之 QtScript
  9. 如何快速学习bootstrap
  10. java String的各种方法及操作
  11. list不是模板
  12. Volterra方程的不动点
  13. 前端的重要部分js
  14. Eclipse的bug,SunTlsRsaPremasterSecret KeyGenerator not available
  15. thinkphp签到的实现代码
  16. PCL系列——怎样逐渐地配准一对点云
  17. Day 3 学习笔记
  18. GDOI2015小Z的旅行路线
  19. 【Atcoder】ARC088 D - Wide Flip
  20. optparse模块

热门文章

  1. Replication:事务复制 Transaction and Command
  2. ER图VISIO 引入Mysql 反向工程
  3. LINUX 下.NET Core 微服务部署实战
  4. mvc中hangfire全局简单配置
  5. Vert.x Web
  6. Mybatis事物浅谈
  7. spark存储模块之内存存储--MemeoryStore
  8. 工具sublime安装
  9. odex vdex art区别
  10. MySQL 5.7数据库参数优化