将 LINQ 查出来的数据反射成DataTable
来源:互联网 发布:全球化 知乎 编辑:程序博客网 时间:2024/04/28 06:11
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Reflection;namespace CodeFristDemoForm{ /// <summary>/// Convert Generic List Type to DataTable./// </summary> public static class List2DataTable { #region "Convert Generic List to DataTable" /// <summary> /// Convert a List{T} to a DataTable. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="items"></param> /// <returns></returns> public static DataTable ConvertToDataTable<T>(this List<T> items) { var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props) { Type t = GetCoreType(prop.PropertyType); tb.Columns.Add(prop.Name, t); } foreach (T item in items) { var values = new object[props.Length]; for (int i = 0; i < props.Length; i++) { values[i] = props[i].GetValue(item, null); } tb.Rows.Add(values); } return tb; } /// <summary> /// Determine of specified type is nullable /// </summary> /// <param name="t"></param> /// <returns></returns> public static bool IsNullable(Type t) { return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)); } /// <summary> /// Return underlying type if type is Nullable otherwise return the type. /// </summary> /// <param name="t"></param> /// <returns></returns> public static Type GetCoreType(Type t) { if (t != null && IsNullable(t)) { if (!t.IsValueType) { return t; } else { return Nullable.GetUnderlyingType(t); } } else { return t; } } #endregion }}
0 0
- 将 LINQ 查出来的数据反射成DataTable
- 将查出来的多个DataTable 合并成一个
- 导入批量数据一次查询将数据的编码重复的查出来
- 【详解】将查出来的元组数据作参数带入
- 将MYSQL查出来的数据从三列转化为一列
- 用linq来将数据源进行排序(DataTable)
- LINQ方式 把DataTable的数据 生成全新的DataTable
- 从WS查出的 RESULT 来进行数据 格式转换 组装成MAP
- 数据库查出来的数据可直接JSON格式化
- Bug:数据库的有数据,但是查出来是0
- 数据库查出来的数据到JAVA中就错了?不可能!
- 将SQL查出的数据导入一个文件
- 将dataTable数据构造成Json格式的字符串
- 将DataTable数据转换成JSON数据
- 使用反射+泛型方法来处理Linq的修改
- 将GridView的数据生成DataTable
- C# 将DataTable的数据导出
- 将表中的字段名以及对应的注释查出来
- 深入了解Struts2返回JSON数据的原理及具体应用范例
- 搭建Python开发环境, Pycharm5注册方式
- swift--- uitoolbar
- 每天一个linux命令(19):find 命令概览
- POJ 2533 Longest Ordered Subsequence(dp LIS)
- 将 LINQ 查出来的数据反射成DataTable
- struts2--上传总结(限制大小和类型 非法上传的跳转)
- leetcode系列(6)Max Points on a Line
- 打印一个数的约数
- adb概览及协议参考
- 【实习第一周】spring入门
- linux查找目录下的所有文件中是否含有某个字符串
- nyoj 523 亡命逃窜 【BFS】
- Write operations are not allowed in read-only