IEnumerable作为DataTable性能问题 [英] IEnumerable as DataTable performance issue

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

问题描述

我有以下扩展名,它从 IEnumerable 中生成 DataTable

I have the following extension, which generates a DataTable from an IEnumerable:

    public static DataTable AsDataTable<T>(this IEnumerable<T> enumerable)
    {
        DataTable table = new DataTable();

        T first = enumerable.FirstOrDefault();
        if (first == null)
            return table;

        PropertyInfo[] properties = first.GetType().GetProperties();
        foreach (PropertyInfo pi in properties)
            table.Columns.Add(pi.Name, pi.PropertyType);

        foreach (T t in enumerable)
        {
            DataRow row = table.NewRow();
            foreach (PropertyInfo pi in properties)
                row[pi.Name] = t.GetType().InvokeMember(pi.Name, BindingFlags.GetProperty, null, t, null);
            table.Rows.Add(row);
        }

        return table;
    }

然而,在大量数据上,性能不是很好。有没有明显的性能修复,我无法看到?

However, on huge amounts of data, the performance isn't very good. Is there any obvious performance fixes that I'm unable to see?

推荐答案

而不是做:

row[pi.Name] = t.GetType().InvokeMember(pi.Name, BindingFlags.GetProperty, null, t, null);

使用:

row[pi.Name] = pi.GetValue(t, null);

这篇关于IEnumerable作为DataTable性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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