在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享。

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Wolfy.List2DataTable
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> lst = new List<Person>()
            {
                , Gender=false, Name="wolfy1"},
                 , Gender=false, Name="wolfy2"},
                  , Gender=false, Name="wolfy3"},
                   , Gender=false, Name="wolfy4"},
                    , Gender=false, Name="wolfy5"},
            };
            DataTable dt = List2DataTable<Person>(lst);
            Console.WriteLine("转换结束");
            Console.Read();
        }
        /// <summary>
        /// 将泛型集合转换为datatable
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entities"></param>
        /// <returns></returns>
        static DataTable List2DataTable<TEntity>(List<TEntity> entities)
        {
            if (entities == null)
            {
                throw new ArgumentNullException("转换的集合为空");
            }
            Type type = typeof(TEntity);
            PropertyInfo[] properties = type.GetProperties();
            DataTable dt = new DataTable(type.Name);
            foreach (var item in properties)
            {
                dt.Columns.Add(new DataColumn(item.Name) { DataType = item.PropertyType });
            }
            foreach (var item in entities)
            {
                DataRow row = dt.NewRow();
                foreach (var property in properties)
                {
                    row[property.Name] = property.GetValue(item);
                }
                dt.Rows.Add(row);
            }
            return dt;
        }
    }
    public class Person
    {
        public int ID { set; get; }
        public string Name { set; get; }
        public bool Gender { set; get; }
    }
}

泛型集合转换为DataTable的更多相关文章

  1. [工具类]泛型集合转换为DataTable

    写在前面 在实际项目中,用到了将集合转换为DataTable,就试着封装了一个方法,记录一下. 代码 using System; using System.Collections.Generic; u ...

  2. C#基础知识之泛型集合转换为DataTable

    在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...

  3. 泛型集合、datatable常用数据类型转换Json帮助类

    泛型集合.datatable常用数据类型转换Json帮助类 using System; using System.Data; using System.Configuration; using Sys ...

  4. linq之将IEnumerable<T>类型的集合转换为DataTable类型 (转载)

    在考虑将表格数据导出成excel的时候,网上搜的时候找到一个特别合适的公共方法,可以将query查询出来的集合转换为datatable 需引用using System.Reflection; publ ...

  5. 使用泛型集合取代datatable作为返回值实现面向对象

    开会的时候,师父说.我们在机房重构时,尽量不要用datatable作为返回值.改用泛型集合的方式,这样能够实现真正的面向对象. 通过查资料和同学交流,把这个问题给攻克了. 对于泛型集合.我也有了一些认 ...

  6. C#;DataTable添加列;DataTable转List泛型集合;List泛型集合转DataTable泛型集合;

    给DataTable添加列 string sql = "select * from cgpmb order by code"; DataTable dt = Bobole.Data ...

  7. c#将list集合转换为datatable的简单办法

    public static class ExtensionMethods        {        /// <summary>        /// 将List转换成DataTabl ...

  8. C# 集合转换为DataTable

    该类就用了几个类型,如int,int?,string,所以其它类型就先没管. 用到的类: public class tb_Projects { public int ProID { get; set; ...

  9. List泛型集合转DataTable

    自存,此方法可以防止出现DataSet不支持System.Nullable的错误 public static DataTable ToDataTable<T>(IEnumerable< ...

随机推荐

  1. 微信红包签名算法 C#代码实现

    string stringA = "appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_id=100001 ...

  2. [SQL] cast 与 convert 都在什么情况下使用

    CONVERT(data_type,expression[,style]) 说明: 此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,ch ...

  3. linux 多网卡 跃点数

    centos6.4 配置两块网卡,eth0设置静态IP,8网段,eth1无线配置dhcp,都是开机启动. 但是eth1无线网卡一旦连接至开放网络(需要web登陆),就替换了之前eth0配置的默认网关, ...

  4. Altium designer 原理图库快速创建

    Altium designer 原理图库快速创建,原来都没发现用这个功能,最近查了一下很好用,就是通过Excel编写管脚名称再直接导入就可以了,很方便的. 1.首先在Excel创建填好对应管脚名称. ...

  5. git remove cache

    若在提交.gitignore之前,不小心提交了无用的文件入repo,可以用以下命令在repo中去除这些文件 git rm -r --cached <filename or .> git a ...

  6. 剖析Disruptor:为什么会这么快?(二)神奇的缓存行填充

    原文链接:http://mechanitis.blogspot.com/2011/07/dissecting-disruptor-why-its-so-fast_22.html 需FQ 计算机入门   ...

  7. Nodejs文件服务器

    最近一直在忙于一个比较大的项目,在项目中需要有个文件服务器来支持.老鸟们建议我去用NodeJs来实现,我在接手这个项目之前其实并不了解NodeJs,但是一直想去了解.借着这个机会好好去学习一下.下面是 ...

  8. 模糊查询(LIKE)and (PATINDEX() . CHARINDEX())

    SQL中的模糊查询一般来说使用模糊查询,大家都会想到LIKE  select * from table where a like '%字符%' 如果一个SQL语句中用多个 like模糊查询,并且记录条 ...

  9. C++ 什么是句柄?为什么会有句柄?HANDLE

    出处:http://www.cppblog.com/mymsdn/archive/2009/02/19/handle-in-windows.html 从广义上,能够从一个数值拎起一大堆数据的东西都可以 ...

  10. 帕金森定律(Parkinson's Law)

    帕金森定律(Parkinson's Law)是官僚主义或官僚主义现象的一种别称, 是由英国历史学家.政治学家西里尔·诺斯古德·帕金森(Cyril Northcote Parkinson)通过长期调查研 ...