public class WebUtil

{
/// <summary>

/// 转换IList<T>为DataTable/// </summary> /// <typeparam name="T">泛型类型</typeparam> /// <param name="list">泛型List集合</param> /// <returns>Datatable 对象</returns>public static DataTable ConvertToDataTable<T>(IList<T> list)

 {

DataTable table = CreateTable<T>();


Type entityType = typeof(T);PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);


foreach (T item in list)

 {

DataRow row = table.NewRow();


foreach (PropertyDescriptor prop in properties)

 {
 row[prop.Name] = prop.GetValue(item);
 }

 table.Rows.Add(row);
 }

return table;

 }

public static DataTable CreateTable<T>()

 {

Type entityType = typeof(T);DataTable table = new DataTable(entityType.Name);PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);



foreach (PropertyDescriptor prop in properties)

 {

//解决 DataSet不支持System.Nullable<>”的异常Type colType = prop.PropertyType;if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))

 {
 colType = colType.GetGenericArguments()[0];
 }
 table.Columns.Add(prop.Name, colType);
 }

return table;

 }
}

//在调用上面的那个类的方法时出现 “DataSet不支持System.Nullable<>”的异常。下面是调用的方式。

IList<PersonInfo> list = GetPersonInfoList();//这是我的列表获取方法。。这里没问题的!!有数据得到!
var dt = WebUtil.ConvertTo<PersonInfo>(list);

C# IList<T>转为DataTable的更多相关文章

  1. .net 打开Excel文档并转为DataTable

    /// <summary> /// 打开Excel文档并转为DataTable /// </summary> /// <returns></returns&g ...

  2. 把List<string>转为DataTable

    //把List<string>转为DataTable List<string> myList = new List<string>(); DataTable dt2 ...

  3. DataTable.Select筛选过滤数据返回DataRow[]转为DataTable添加到DataSet

    问题还原,如图所示,我们要筛选所有SHDP 为北京翠微KR的数据. 1. 筛选DataTable微软为我们提供了一个方法DataTable.Select(),其用法如下: 1)  Select()—— ...

  4. C# List<T>转为 DataTable

    // remove "this" if not on C# 3.0 / .NET 3.5 public static System.Data.DataTable ConvertTo ...

  5. C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表

    调用word的com组件将400条数据导入word表格中耗时10分钟简直不能忍受,使用NPOI组件耗时4秒钟.但是NPOI中替换书签内容的功能不知道是不支持还是没找到. 辅助类 Excel表格数据与D ...

  6. 泛型类型转为DataTable类型

    public static DataTable ConvertToDatatable<T>(IEnumerable<T> data) { PropertyDescriptorC ...

  7. 读取Excel里面的内容转为DataTable

    using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using Sy ...

  8. List转为DataTable并可以导出为Excel

    using com.jd120.Core.Utility; using System; using System.Collections.Generic; using System.Data; usi ...

  9. 将 xml 文件 转为 DataTable

    private static DataTable CreateDataTable(string table) { DataSet dataSet = new DataSet(); string dat ...

随机推荐

  1. LTIB常用命令1

    下面再写一点ltib的常用命令参数吧,虽然觉得对其编译内核和文件系统流程有了一定了解,但是对其命令参数用过的还不是很多,可以说是不甚了解,下面介绍一些,希望有用: 首先一个比较有用的命令参数就是hel ...

  2. easyui 上传文件代码

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;usi ...

  3. git 10.8

    git clone xxxx.git生成一个本地的文件夹acd agit checkout -b abcgit checkout mastergit pull然后数据全部由更新 但是是远程的更新 不能 ...

  4. Windows不重启就使环境变量修改生效

    以修改环境变量“PATH”为例,修改完成后,进入DOS命令提示符,输入:set PATH=C: ,关闭DOS窗口.再次打开DOS窗口,输入:echo %PATH% ,可以发现“我的电脑”->“属 ...

  5. windows redis:Uncaught exception 'RedisException' with message 'Redis server went away'

    window-exe-redis-2.8.12服务,当你复制好php_igbinary.dll,php_redis.dll时候,你运行redis报错:Fatal error: Uncaught exc ...

  6. 【BZOJ 2818】Gcd - 筛法求素数&phi()

    题目描述 给定整数,求且为素数的数对有多少对. 分析 首先筛出所有的素数. 我们考虑枚举素数p,统计满足的个数,等价于统计的个数,即统计以内满足互质的有序数对个数. 不难发现,也就是说,我们只要预处理 ...

  7. is(C# 参考)

    检查对象是否与给定类型兼容. 例如,下面的代码可以确定对象是否为 MyObject 类型的一个实例,或者对象是否为从 MyObject 派生的一个类型:     if (obj is MyObject ...

  8. js高级程序设计(三)基本概念

    数据类型 ECMAscript中有五种简单数据类型Undefined,Null,Boolean,Number,String 还有一种复杂数据类型Object. typeof操作符 typeof可能返回 ...

  9. CentOS6.5配置python开发环境之一:CentOS图形化界面显示

    这两天在配置centos系统下python的开发环境和工具. 刚用centos,做做记录可以方便以后有需要的人...查资料确实挺麻烦的 centos6.5 sublime3 python27 subl ...

  10. uva----(100)The 3n + 1 problem

     The 3n + 1 problem  Background Problems in Computer Science are often classified as belonging to a ...