转换IEnumerable< T>列出< T>在LINQ结果上,巨大的性能损失

转换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.