-
DataTable转成实体列表 和 DataRow转成实体类
1 #region DataTale转为实体列表 2 /// <summary> 3 /// DataTale转为实体列表 4 /// </summary> 5 /// <typeparam name="T">实体类类型</typeparam> 6 /// <param name="table">DataTable</param> 7 /// <returns>List<T></returns> 8 public List<T> DataTableToModelList<T>(DataTable table) 9 { 10 List<T> list = new List<T>(); 11 T t = default(T); 12 PropertyInfo[] propertypes = null; 13 string tempName = string.Empty; 14 foreach (DataRow row in table.Rows) 15 { 16 t = Activator.CreateInstance<T>(); 17 propertypes = t.GetType().GetProperties(); 18 foreach (PropertyInfo pro in propertypes) 19 { 20 tempName = pro.Name; 21 if (table.Columns.Contains(tempName)) 22 { 23 object value = row[tempName]; 24 if (value.GetType() == typeof(System.DBNull)) 25 { 26 value = null; 27 } 28 pro.SetValue(t, value, null); 29 } 30 } 31 list.Add(t); 32 } 33 return list; 34 } 35 #endregion 36 37 #region DataRow转为实体类 38 /// <summary> 39 /// DataRow转为实体类 40 /// </summary> 41 /// <typeparam name="T">实体类类型</typeparam> 42 /// <param name="row">DataRow</param> 43 /// <returns>T</returns> 44 public T DataRowToModel<T>(DataRow row) 45 { 46 47 T t = default(T); 48 PropertyInfo[] propertypes = null; 49 string tempName = string.Empty; 50 t = Activator.CreateInstance<T>(); 51 propertypes = t.GetType().GetProperties(); 52 foreach (PropertyInfo pro in propertypes) 53 { 54 tempName = pro.Name; 55 if (row.Table.Columns.Contains(tempName)) 56 { 57 object value = row[tempName]; 58 if (value.GetType() == typeof(System.DBNull)) 59 { 60 value = null; 61 } 62 pro.SetValue(t, value, null); 63 } 64 } 65 return t; 66 } 67 #endregion
栏目列表
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比