zoukankan
html css js c++ java
DataTable转成List集合
项目开发中,经常会获取到DataTable对象,如何把它转化成一个List对象呢?前几天就碰到这个问题,网上搜索整理了一个万能类,用了泛型和反射的知识。共享如下:
按 Ctrl+C 复制代码
public class ModelConvertHelper<T> where T : new() // 此处一定要加上new() { public static IList<T> ConvertToModel(DataTable dt) { IList<T> ts = new List<T>();// 定义集合 Type type = typeof(T); // 获得此模型的类型 string tempName = ""; foreach (DataRow dr in dt.Rows) { T t = new T(); PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性 foreach (PropertyInfo pi in propertys) { tempName = pi.Name; if (dt.Columns.Contains(tempName)) { if (!pi.CanWrite) continue; object value = dr[tempName]; if (value != DBNull.Value) pi.SetValue(t, value, null); } } ts.Add(t); } return ts; } }
按 Ctrl+C 复制代码
查看全文
相关阅读:
响应式css样式
组件 computed 与 vuex 中 getters 的使用,及 mapGetters 的使用,对象上追加属性,合并对象
nginx 错误集锦
动态的添加路由
NProgress的使用 及 路由 token 定向的使用
token的解码及 判断值不为空的方法
nginx 的使用
IT公司100题-tencent-打印所有高度为2的路径
测试
Objective-C 与 C++ 的异同
原文地址:https://www.cnblogs.com/yelanggu/p/6980493.html
最新文章
在拖拽元素的时候,如果元素内部加了文字或图片,拖拽效果会失灵?
正则
Date对象
math对象的属性和方法
事件对象,arguments、事件的兼容问题
闭包、点赞案列
作用域面试题
ajax
2019年第一篇博客 面向对象
Date 日期对象
热门文章
Math 数值对象
RegExp 正则
函数闭包
作用域面试题
事件
bom
js之节点获取
淘宝rem适配方案
粘性定位 sticky
bootstrap 上下页滚动
Copyright © 2011-2022 走看看