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

查看:59
本文介绍了转换IEnumerable< 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<T>确实很慢,它会使列表可变吗,因此转换很慢?

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?

IEnumerable中的1000万条记录上,.ToList<T>大约需要6秒钟.

On 10 million 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.

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

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