LIst和table的转换
- public static class DataTableExtensions
- {
- /// <summary>
- /// 转化一个DataTable
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="list"></param>
- /// <returns></returns>
- public static DataTable ToDataTable<T>(this IEnumerable<T> list)
- {
- //创建属性的集合
- List<PropertyInfo> pList = new List<PropertyInfo>();
- //获得反射的入口
- Type type = typeof(T);
- DataTable dt = new DataTable();
- //把所有的public属性加入到集合 并添加DataTable的列
- Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
- foreach (var item in list)
- {
- //创建一个DataRow实例
- DataRow row = dt.NewRow();
- //给row 赋值
- pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
- //加入到DataTable
- dt.Rows.Add(row);
- }
- return dt;
- }
- /// <summary>
- /// DataTable 转换为List 集合
- /// </summary>
- /// <typeparam name="TResult">类型</typeparam>
- /// <param name="dt">DataTable</param>
- /// <returns></returns>
- public static List<T> ToList<T>(this DataTable dt) where T : class, new()
- {
- //创建一个属性的列表
- List<PropertyInfo> prlist = new List<PropertyInfo>();
- //获取TResult的类型实例 反射的入口
- Type t = typeof(T);
- //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表
- Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); });
- //创建返回的集合
- List<T> oblist = new List<T>();
- foreach (DataRow row in dt.Rows)
- {
- //创建TResult的实例
- T ob = new T();
- //找到对应的数据 并赋值
- prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); });
- //放入到返回的集合中.
- oblist.Add(ob);
- }
- return oblist;
- }
- /// <summary>
- /// 将集合类转换成DataTable
- /// </summary>
- /// <param name="list">集合</param>
- /// <returns></returns>
- public static DataTable ToDataTableTow(IList list)
- {
- DataTable result = new DataTable();
- if (list.Count > 0)
- {
- PropertyInfo[] propertys = list[0].GetType().GetProperties();
- foreach (PropertyInfo pi in propertys)
- {
- result.Columns.Add(pi.Name, pi.PropertyType);
- }
- for (int i = 0; i < list.Count; i++)
- {
- ArrayList tempList = new ArrayList();
- foreach (PropertyInfo pi in propertys)
- {
- object obj = pi.GetValue(list[i], null);
- tempList.Add(obj);
- }
- object[] array = tempList.ToArray();
- result.LoadDataRow(array, true);
- }
- }
- return result;
- }
- /**/
- /// <summary>
- /// 将泛型集合类转换成DataTable
- /// </summary>
- /// <typeparam name="T">集合项类型</typeparam>
- /// <param name="list">集合</param>
- /// <returns>数据集(表)</returns>
- public static DataTable ToDataTable<T>(IList<T> list)
- {
- return ToDataTable<T>(list, null);
- }
- /**/
- /// <summary>
- /// 将泛型集合类转换成DataTable
- /// </summary>
- /// <typeparam name="T">集合项类型</typeparam>
- /// <param name="list">集合</param>
- /// <param name="propertyName">需要返回的列的列名</param>
- /// <returns>数据集(表)</returns>
- public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)
- {
- List<string> propertyNameList = new List<string>();
- if (propertyName != null)
- propertyNameList.AddRange(propertyName);
- DataTable result = new DataTable();
- if (list.Count > 0)
- {
- PropertyInfo[] propertys = list[0].GetType().GetProperties();
- foreach (PropertyInfo pi in propertys)
- {
- if (propertyNameList.Count == 0)
- {
- result.Columns.Add(pi.Name, pi.PropertyType);
- }
- else
- {
- if (propertyNameList.Contains(pi.Name))
- result.Columns.Add(pi.Name, pi.PropertyType);
- }
- }
- for (int i = 0; i < list.Count; i++)
- {
- ArrayList tempList = new ArrayList();
- foreach (PropertyInfo pi in propertys)
- {
- if (propertyNameList.Count == 0)
- {
- object obj = pi.GetValue(list[i], null);
- tempList.Add(obj);
- }
- else
- {
- if (propertyNameList.Contains(pi.Name))
- {
- object obj = pi.GetValue(list[i], null);
- tempList.Add(obj);
- }
- }
- }
- object[] array = tempList.ToArray();
- result.LoadDataRow(array, true);
- }
- }
- return result;
- }
- }
LIst和table的转换的更多相关文章
- 如何将jsp页面的table报表转换到excel报表导出
假设这就是你的jsp页面: 我们会添加一个“导出到excel”的超链接,它会把页面内容导出到excel文件中.那么这个页面会变成这个样子 在此,强调一下搜索时关键词的重要性,这样一下子可以定位到文章, ...
- sp 数据拼接html table表转换xml,发邮件
USE [BES_ADV] GO /****** Object: StoredProcedure [dbo].[RSP_FN_UNAPPLIED_Mail_Reminder] Script Date: ...
- Bootstrap Table的例子(转载)
转载自:http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html#classes-table 使用的API: data1.json da ...
- Oracle数据库中日期/数字和字符之间的转换和计算
--查出当前系统时间 select SYSDATE from table; --格式转换 -- TO_CHAR 把日期或数字转换为字符串 -- TO_CHAR(number, '格式') -- TO_ ...
- 【翻译】Flink Table Api & SQL —Streaming 概念 ——时间属性
本文翻译自官网: Time Attributes https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/str ...
- python运算符
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcIAAAHCCAIAAADzel4SAAAgAElEQVR4Aey9+bMcSXLnV1dmna/ejR
- 利用Servlet导出Excel
-----因为Excel可以打开HTML文件,因此可以利用页面的Form表单把页面中的table内容提交给Servlet,然后后台把提交上来的table内容转换成文件流的形式,并以下载的形式转给客户端 ...
- 数据库多表连接方式介绍-HASH-JOIN
1.概述 hash join是一种数据库在进行多表连接时的处理算法,对于多表连接还有两种比较常用的方式:sort merge-join 和 nested loop. 为了比较清楚的介绍hash joi ...
- 在SQL中使用CLR提供基本函数对二进制数据进行解析与构造
二进制数据包的解析一般是借助C#等语言,在通讯程序中解析后形成字段,再统一单笔或者批量(表类型参数)提交至数据库,在通讯程序中,存在BINARY到struct再到table的转换. 现借助CLR提 ...
随机推荐
- 51nod 算法马拉松4 B递归(YY)
递归 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 函数f(n,m) { 若n=1或m=1返回a[n][m]; 返回f(n-1,m)异或f(n,m-1); } 读入2<= ...
- cocostudio 使用教程
项目配置 http://blog.csdn.net/chinahaerbin/article/details/21559351 项目配置如下: 还要引入命名空间之类的: #include " ...
- 为什么 Windows 10 无论怎么设置一分钟后就自动关屏幕?
为什么 Windows 10 无论怎么设置一分钟后就自动关屏幕? 在设置中设置了很多方法,但不管怎么设置就是不行,不到一分钟一定关屏幕. 开始以为是能源之星引起,查了相关资料说不是,那个能源之星标志只 ...
- Java9的新特性
2017.9.21延期了好几次的Java9正式发布,在人工智能的时代,java还能不能持续辉煌是个问题.看看java9的新特性没什么让自己想升级的意愿,因为要么时一些特性用不到,要么时已经有其它方案代 ...
- postgresql双机热备、高可用方案(采用pacemaker+corosync实现)
http://blog.csdn.net/qguanri/article/details/51151974 需求描述 我们有两台centos7的数据库主机A.B.要对A.B实现双机热备,A作为数据库m ...
- bzoj 5020(洛谷4546) [THUWC 2017]在美妙的数学王国中畅游——LCT+泰勒展开
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5020 https://www.luogu.org/problemnew/show/P4546 ...
- 希尔排序算法-python实现
#-*- coding: UTF-8 -*- import numpy as np def ShellSort(a): gap = a.size / 2 while gap >= 1: for ...
- Linux Namespace
转载请注明出处,并保留以上所有对文章内容.图片.表格的来源的描述. 一.Linux Namespace Linux Namespace是Linux提供的一种OS-level virtualizatio ...
- WCF揭秘学习笔记(2):数据表示
背景知识 WCF提供了一种语言为软件通信建模,称作服务模型.使用更底层的编程架构提供的类可以从这种语言建立的模型中生成可用的通信软件. 在服务模型使用的语言中,负责通信的软件部分称为服务(servic ...
- Java-Runoob-高级教程-实例-字符串:04. Java 实例 - 字符串替换
ylbtech-Java-Runoob-高级教程-实例-字符串:04. Java 实例 - 字符串替换 1.返回顶部 1. Java 实例 - 字符串替换 Java 实例 如何使用java替换字符串 ...