The method below converts an array of objects to a DataTable object in C#.

http://www.c-sharpcorner.com/blogs/dynamic-objects-conveting-into-data-table-in-c-sharp1

public static DataTable GetDataTableFromObjects(object[] objects)
{
    if (objects != null && objects.Length > 0)
    {
        Type t = objects[0].GetType();
        DataTable dt = new DataTable(t.Name);
        foreach (PropertyInfo pi in t.GetProperties())
        {
            dt.Columns.Add(new DataColumn(pi.Name));
        }
        foreach (var o in objects)
        {
            DataRow dr = dt.NewRow();
            foreach (DataColumn dc in dt.Columns)
            {
                dr[dc.ColumnName] = o.GetType().GetProperty(dc.ColumnName).GetValue(o, null);
            }
            dt.Rows.Add(dr);
        }
        return dt;
    }
    return null;
}
        /// <summary>
        /// ToDataTable
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="items">实体集</param>
        /// <returns></returns>
        public static DataTable ToDataTable<T>(this IEnumerable<T> items)
        {
            var tb = new DataTable(typeof(T).Name);

            PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var prop in props)
            {
                tb.Columns.Add(prop.Name, prop.PropertyType);
            }

            foreach (var item in items)
            {
                var values = new object[props.Length];
                for (var i = 0; i < props.Length; i++)
                {
                    values[i] = props[i].GetValue(item, null);
                }

                tb.Rows.Add(values);
            }

            return tb;
        }
时间: 2024-10-17 06:17:17

The method below converts an array of objects to a DataTable object in C#.的相关文章

Array of Objects

You should be comfortable with the content in the modules up to and including the module "Arrays" for this project. Create a class called consultCo that holds a private class called employee that contains the name, pay rate and social security n

Why does typeof array with objects return “Object” and not “Array”?

https://stackoverflow.com/questions/4775722/check-if-object-is-an-array One of the weird behaviour and spec in Javascript is the typeof Array is Object. You can check if the variable is an array in couple of ways: var isArr = data instanceof Array; v

js 中 new Array() var a={} var a=[] new Object()

var a={}; new Object(); 表示定义一个空对象: new Array(); var a=[]; 表示定义一个空数组: 其他: 数组表示有序数据的集合,而对象表示无序数据的集合: 在Javascript语言中,关联数组就是对象,对象就是关联数组.这一点与php语言完全不同,在php中,关联数组也是数组. 单个对象是没有length属性的,所以不能a.length求长度:数组可以:

Zend API:深入 PHP 内核

Introduction Those who know don't talk. Those who talk don't know. Sometimes, PHP "as is" simply isn't enough. Although these cases are rare for the average user, professional applications will soon lead PHP to the edge of its capabilities, in t

类加载器深入理解和双亲委托模型的案例分析

类加载器深入理解和双亲委托模型的案例分析 我们知道类必须通过类加载器加载后,我们程序才可以使用.接下来我们就对类加载器进行分析,Java虚拟机的类加载器是如何加载类的.首先我们可以从ClassLoader的源码分析入手. ClassLoader 的源码分析 ClassLoader 的javadoc文档 javadoc文档是最权威的官方讲解,可以对ClassLoader有一个比较全面且正确的一个认知.下面是javadoc内容. A class loader is an object that is

PHP 使用用户自定义的比较函数对数组中的值进行排序

原文:PHP 使用用户自定义的比较函数对数组中的值进行排序 usort (PHP 4, PHP 5) usort —      使用用户自定义的比较函数对数组中的值进行排序 说明 bool usort        ( array &$array       , callable $cmp_function       ) 本函数将用用户自定义的比较函数对一个数组中的值进行排序.如果要排序的数组需要用一种不寻常的标准进行排序,那么应该使用此函数. Note: 如果两个成员比较结果相同,则它们在排

[Quote] Java - Generics

From http://www.tutorialspoint.com/java/java_generics.htm It would be nice if we could write a single sort method that could sort the elements in an Integer array, a String array or an array of any type that supports ordering. Java Generic methods an

asp.net Hierarchical Data

Introduction A Hierarchical Data is a data that is organized in a tree-like structure and structure allows information to be stored in a parent-child relationship with one-to-many relation records. This data can be stored either in a single table or

设计模式: 自己手动写一个代理模式

代理模式:为另一个对象提供一个替身或占位符以访问这个对象.代理模式为另一个对象提供代表,以便控制客户对对象的访问,管理访问的方式有许多种. 远程代理管理客户和远程对象之间的交互. 虚拟代理控制访问实例化开销大的对象. 保护代理基于调用者控制对对象方法的访问. 代理模式有许多变体,例如:缓存代理.同步代理.防火墙代理.写入时复制代理. Java内置的代理支持,可以根据需要动态创建代理,并将所有调用分配到所选的调用处理器(InvocationHandler). 下面 的例子是一种保护代理 类图: 源