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提 ...
随机推荐
- C语言--第四次作业
作业要求一 (70分) 实践最简答的项目wordcount,必须完成其中的基本功能,若可以完成其他功能给予加分.完成后请将你的设计思路.主要代码写在本次作业博客里. 真的迷茫<(_ _)> ...
- Oracle单表去重复(一)
去重有两层含义,一:是记录完全一样:二:是符合一定条件的认为是重复. 根据表的数量,去重可划分为:单表去重和多表关联去重. 对于去重,一般最容易想到的是用distinct,而distinct只能对 ...
- java局部变量和临时变量
局部变量:temp=1, 临时变量:return a+b 临时变量会有一点的性能优势 局部变量会比成员变量和静态成员变量有优势,改进的方法是吧成员变量和静态成员变量赋值在局部变量:https://bl ...
- Android 基础题目
1. BroadcastReceiver 在UI thread? BroadcastReceiver 总是在UI thread, If you register your BroadcastRecei ...
- git push 免密码
git push 免密码 通用情况 使用ssh协议 git add 使用tab键自动补全的中文文件名乱码 jupyter notebook 创建密码 git push 免密码 通用情况 1.使用文件创 ...
- 微信小程序获取用户openid,头像昵称信息,后台java代码
https://blog.csdn.net/qq_39851704/article/details/79025557
- Java 获取字符串指定下标位置的值 charAt()
Java手册 charAt public char charAt(int index) 返回指定索引处的 char 值.索引范围为从 0 到 length() - 1.序列的第一个 char 值位于索 ...
- 32位汇编基础_内存_每个应用进程都会有自己独立的4GB内存空间
1.每个应用进程都会有自己独立的4GB内存空间 这句话很多人听起来可能会很矛盾很不解. 例如,我的电脑只有2GB的内存,打开个软件机会占用4GB内存,而我的电脑内存只有2GB,显然不够用,但是为什么程 ...
- DP系列——树形DP(Codeforces543D-Road Improvement)
一.题目链接 http://codeforces.com/problemset/problem/543/D 二.题意 给一棵树,一开始所有路都是坏的.询问,以每个节点$i$为树根,要求从树根节点到其他 ...
- user_add示例
#!/usr/bin/python3# -*- coding: utf-8 -*-# @Time : 2018/5/28 16:51# @File : use_test_add.py 数据 ...