转换IEnumberable< T>列出< T>在LINQ的结果,巨大的性能损失 [英] Converting IEnumberable<T> to List<T> on a LINQ result, huge performance loss

查看:131
本文介绍了转换IEnumberable< T>列出< T>在LINQ的结果,巨大的性能损失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个LINQ的结果,你喜欢这样的:

On a LINQ-result you like this:

var result = from x in Items select x;
List<T> list = result.ToList<T>();

不过,了ToList&LT; T&GT; 实在是太慢了,它榜上无名可变的,因此,转换速度慢?

However, the ToList<T> is Really Slow, does it make the list mutable and therefore the conversion is slow?

在大多数情况下,我能设法只是我的的IEnumerable Paralell.DistinctQuery ,但现在我想项目绑定到一个DataGridView,所以因此我需要的不是的IEnumerable ,建议我如何将获得的了ToList或更换性能?

In most cases I can manage to just have my IEnumerable or as Paralell.DistinctQuery but now I want to bind the items to a DataGridView, so therefore I need to as something else than IEnumerable, suggestions on how I will gain performance on ToList or any replacement?

在10畅想记录在的IEnumerable .ToList&LT; T&GT; 需要6秒左右。

On 10 milion records in the IEnumerable, the .ToList<T> takes about 6 seconds..

推荐答案

.ToList()是比较慢的什么的?

如果你是比较

var result = from x in Items select x;
List<T> list = result.ToList<T>();

var result = from x in Items select x;

您应该注意,由于查询被懒洋洋地评估,第一行并没有做很多的。它不检索任何记录。 延迟执行的使这种比较的完全不公平的的。

you should note that since the query is evaluated lazily, the first line doesn't do much at all. It doesn't retrieve any records. Deferred execution makes this comparison completely unfair.

这篇关于转换IEnumberable&LT; T&GT;列出&LT; T&GT;在LINQ的结果,巨大的性能损失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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