将IEnumerable转换为DataTable [英] Convert IEnumerable to DataTable

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

问题描述



我可以使用反射来获取属性和值,但这似乎有点低效,是否有内置的东西?



(我知道例如:ObtainDataTableFromIEnumerable)



编辑

问题通知我处理空值的问题。

我下面写的代码正确处理空值。

  public static DataTable ToDataTable< T>(这个IEnumerable< T>项目){
//创建结果表,并收集T
的所有属性DataTable table = new DataTable typeof(T).Name);
PropertyInfo [] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

//将属性作为列添加到datatable
foreach(pro prop中的var prop){
Type propType = prop.PropertyType;

//是否为可空类型?获取底层类型
if(propType.IsGenericType&& propType.GetGenericTypeDefinition()。Equals(typeof(Nullable)))
propType = new NullableConverter(propType).UnderlyingType;

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

//将每个T的属性值作为行添加到datatable
foreach(项目中的var项目){
var values = new object [props。长度];
for(var i = 0; i< props.Length; i ++)
values [i] = props [i] .GetValue(item,null);

table.Rows.Add(values);
}

返回表;
}


解决方案

看看这个: a href =http://www.chinhdo.com/20090402/convert-list-to-datatable/ =noreferrer>将列表/ IEnumerable转换为DataTable / DataView



在我的代码中,我将其更改为扩展方法:

  public static DataTable ToDataTable< T> (这个List< T>项目)
{
var tb = new DataTable(typeof(T).Name);

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

foreach(道具中的var prop)
{
tb.Columns.Add(prop.Name,prop.PropertyType);
}

foreach(项目中的项目)
{
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;
}


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转换为DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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