Enumerable转换为DataTable
今天在项目组公共类库中发现一个 Enumerable类型转换为DataTable,写的挺精简的,拿出来跟大家共享一下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
namespace H3C.RD.IPDPlan.Common
{
public static class EnumerableConverterExtension
{
/// <summary>
/// 转换为一个DataTable
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static DataTable ToDataTable<TResult>(this IEnumerable<TResult> value) where TResult : class
{
return value.ToDataTable(Utility.DateTimeFormat.DATETIME_FORMAT_YYYY_MM_DD);
}
/// <summary>
/// 转换为一个DataTable
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static DataTable ToDataTable<TResult>(this IEnumerable<TResult> value,string format) where TResult : class
{
if (string.IsNullOrEmpty(format))
{
format = Utility.DateTimeFormat.DATETIME_FORMAT_YYYY_MM_DD;
}
//创建属性的集合
List<PropertyInfo> pList = new List<PropertyInfo>();
//获得反射的入口
Type type = typeof(TResult); DataTable dt = new DataTable();
//把所有的public属性加入到集合 并添加DataTable的列
Array.ForEach<PropertyInfo>(type.GetProperties(), p =>
{
pList.Add(p);
if (p.PropertyType.IsGenericType)
{
dt.Columns.Add(p.Name);
}
else
{
dt.Columns.Add(p.Name, p.PropertyType);
}
});
if (null != value)
{
foreach (var item in value)
{
//创建一个DataRow实例
DataRow row = dt.NewRow();
//给row 赋值
pList.ForEach(p => row[p.Name] = (p.GetValue(item, null) is DateTime) ? Utility.FormatDateTime(p.GetValue(item, null), format) : p.GetValue(item, null));
//加入到DataTable
dt.Rows.Add(row);
}
}
return dt;
}
}
}
Enumerable转换为DataTable的更多相关文章
- 对象列表转换为DataTable或DataTable转换为对象列表.
/**********************************************************************************/ // 说明: 数据转换工具. ...
- linq之将IEnumerable<T>类型的集合转换为DataTable类型 (转载)
在考虑将表格数据导出成excel的时候,网上搜的时候找到一个特别合适的公共方法,可以将query查询出来的集合转换为datatable 需引用using System.Reflection; publ ...
- Json 字符串 转换为 DataTable数据集合
/// <summary> /// 将json转换为DataTable /// </summary> /// <param name="strJson" ...
- c#常用的Datable转换为json,以及json转换为DataTable操作方法
#region DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...
- [工具类]泛型集合转换为DataTable
写在前面 在实际项目中,用到了将集合转换为DataTable,就试着封装了一个方法,记录一下. 代码 using System; using System.Collections.Generic; u ...
- 泛型集合转换为DataTable
在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...
- 【c#操作office】--OleDbDataAdapter 与OleDbDataReader方式读取excel,并转换为datatable
OleDbDataAdapter方式: /// <summary> /// 读取excel的表格放到DataTable中 ---OleDbDataAdapter /// </summ ...
- C#基础知识之泛型集合转换为DataTable
在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...
- 把List<T>转换为DataTable
下面这个学习,把List<T>转换为Datatable. 下面先创建一个对象T: class Ay { private int _ID; public int ID { get { ret ...
随机推荐
- Linux驱动之定时器在按键去抖中的应用
机械按键在按下的过程中会出现抖动的情况,如下图,这样就会导致本来按下一次按键的过程会出现多次中断,导致判断出错.在按键驱动程序中我们可以这么做: 在按键驱动程序中我们可以这么做来取消按键抖动的影响:当 ...
- js多维数组转一维数组
1,使用map方法 var arr = [1,[2,[[3,4],5],6]]; function unid(arr){ var arr1 = (arr + '').split(',');//将数组转 ...
- Win10电脑系统使用技巧
现如今,电脑已经成为我们不可或缺的伙伴,陪伴着我们的工作.娱乐和生活,而Windows10在大家使用的电脑中占据了大多数,但是很多的小伙伴对它的许多功能并不真正了解,今天小编就带大家了解一下这些技巧, ...
- python基础之Day6
一.元组 定义:t=(1,2,3,4) 总结:存多个值,值为任意类型 只有读的需求,没有改的需求 有序,不可变(元组里每个值对应的索引内存地址不能变) 在元素个数相同的情况下,元组比列表更节省空间 二 ...
- zeromq学习记录(八)负载均衡 附ZMQ_ROUTER的流程分析
/************************************************************** 技术博客 http://www.cnblogs.com/itdef/ ...
- Could not transfer artifact org.springframework
无法从中心仓库获取该版本的信息, 从新下载: 1.配置eclipse中的maven user setting路径为本地maven安装路径 配置阿里云镜像路径 <mirror> <i ...
- Gym - 100781G-Goblin Garden Guards
题目链接:https://nanti.jisuanke.com/t/28882 解题思路:单纯的判断点是否在圆内,一一遍历圆外切正方形内的点即可,注意,该题要建个结构体数组存每个地精的位置,再bool ...
- 常用screen参数
摘自:https://www.cnblogs.com/webnote/p/5749675.html screen -S yourname -> 新建一个叫yourname的sessionscre ...
- 图解HTTP第一章
了解 Web 及网络基础 Web 页面是如何呈现的吗? Web 使用一种名为 HTTP(HyperText Transfer Protocol,超文本传输协议)的协议作为规范,完成从客户端到服务器端等 ...
- kbmmw中向服务器端传递对象的一种简单方式
运行环境:delphi 10.2+kbmmw 5.6.20 在kbmmw 的老版本中,要向服务器传送一个本地的对象,一般都需要进行一些转换,例如通过序列化的方式. 在新版的kbmmw中这一切都变的很简 ...