转换为IEnumerable的数据表 [英] Convert IEnumerable to DataTable

查看:114
本文介绍了转换为IEnumerable的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个IEnumerable转换为一个DataTable的好方法?

我可以使用反射来获取属性和值,但似乎有点低效率的,是有什么内建?

(我知道像例子:ObtainDataTableFromIEnumerable)

修改:结果
问题通知我处理空值的问题。结果
在code我在下面写正确处理空值。

 公共静态数据表ToDataTable< T>(这个IEnumerable的< T>项目){
    //创建结果表,并收集一件T的所有属性
    数据表表=新的DataTable(typeof运算(T).Name点);
    的PropertyInfo [] =道具typeof运算(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);    //栏目添加属性到DataTable
    的foreach(在道具VAR道具){
        键入propType = prop.PropertyType;        //它是一个可空类型?获取基本类型
        如果(propType.IsGenericType&安培;&安培; propType.GetGenericTypeDefinition()等于(typeof运算(可空<>)))
            propType =新NullableConverter(propType).UnderlyingType;        table.Columns.Add(prop.Name,propType);
    }    //添加每个T的属性值作为行的数据表
    的foreach(在项目VAR项){
        VAR值=新对象[props.Length]
        对于(VAR I = 0; I< props.Length;我++)
            值[I] =道具[I] .GetValue(项目,零);        table.Rows.Add(值);
    }    返回表;
}


解决方案

看看这个:转换列表/ IEnumerable的数据表来/数据视图

在我的code我把它改为一个扩展方法:

 公共静态数据表ToDataTable< T>(名单< T>的项目)
{
    VAR TB =新的DataTable(typeof运算(T).Name点);    的PropertyInfo [] =道具typeof运算(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);    的foreach(在道具VAR道具)
    {
        tb.Columns.Add(prop.Name,prop.PropertyType);
    }     的foreach(在项目VAR项)
    {
       VAR值=新对象[props.Length]
        对于(VAR I = 0; I< props.Length;我++)
        {
            值[I] =道具[I] .GetValue(项目,零);
        }        tb.Rows.Add(值);
    }    返回TB;
}

Is there a nice way to convert an IEnumerable to a DataTable?

I could use reflection to get the properties and the values, but that seems a bit inefficient, is there something build-in?

(I know the examples like: ObtainDataTableFromIEnumerable)

EDIT:
This question notified me of a problem handling null values.
The code I wrote below handles the null values properly.

public static DataTable ToDataTable<T>(this IEnumerable<T> items) {  
    // Create the result table, and gather all properties of a T        
    DataTable table = new DataTable(typeof(T).Name); 
    PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);  

    // Add the properties as columns to the datatable
    foreach (var prop in props) { 
        Type propType = prop.PropertyType; 

        // Is it a nullable type? Get the underlying type 
        if (propType.IsGenericType && propType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) 
            propType = new NullableConverter(propType).UnderlyingType;  

        table.Columns.Add(prop.Name, propType); 
    }  

    // Add the property values per T as rows to the datatable
    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);   

        table.Rows.Add(values);  
    } 

    return table; 
} 

解决方案

Look at this one: Convert List/IEnumerable to DataTable/DataView

In my code I changed it into a extension method:

public static DataTable ToDataTable<T>(this List<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;
}

这篇关于转换为IEnumerable的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆