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 ...
随机推荐
- JavaSE基础知识(5)—面向对象(5.1类和对象概念、创建及内存分配)
一.类和对象的相关概念 1.面向对象和面向过程的理解 面向对象和面向过程都属于解决问题的思考方式.面向过程:以执行者的角度思考问题,侧重于“怎么做”,比较适合解决小型项目面向对象:以指挥者的角度思考问 ...
- R 语言安装
在linux下,对于手动安装的软件,当时间长了,我们就会忘记安装这个软件的细节.这就不利于以后软件的卸载工作了.而yum则会帮我们记住相关安装细节,当软件被卸载的时候,没用的文件也会一并被删除.因此, ...
- javaweb开发.调试
一.快速调试一个类 1.类里面写public static void main(String[] args) throws Exception{}方法 2.该类上右键->Run As->J ...
- PHP脚本命令行执行成功,CRON无法执行故障解决记录
先来看看一个最简单的PHP文件(ip.php) <?php $myip = get_ip_cmd(); echo($myip); // get ip address function get_i ...
- tensorflow nan
https://github.com/tensorflow/tensorflow/issues/3212 NaNs usually indicate something wrong with your ...
- hdu-1878(欧拉回路)
题目链接:传送门 思路:就是判断无向图的欧拉回路的两个条件:(1)连通性(2)点的度数是偶数 注意:两个条件一同时满足才行. #include<iostream> #include< ...
- RISC与CISC比较
1.RISC与CISC的差异 处理器的指令集可简单分为2种,CISC(complex instruction set computer)以及RISC(reduced instruction set c ...
- android-glsurfaceview Activity框架程序
两个基本的类让我们使用OpenGL ES API来创建和操纵图形:GLSurfaceView和 GLSurfaceView.Renderer. 1. GLSurfaceView: 这是一个视图类,你可 ...
- 关于esp32的ADC采集
对于ADC采集 程序源码如下: /* ADC1 Example This example code is in the Public Domain (or CC0 licensed, at your ...
- 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件
[源码下载] 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件 作者:webabcd 介绍 ...