问题描述
如果我没记错的话,ToList()方法将对所提供集合的每个元素进行迭代,并将它们添加到List的新实例中并返回该实例.假设一个例子
If I am not wrong, the ToList() method iterate on each element of provided collection and add them to new instance of List and return this instance.Suppose an example
//using linq
list = Students.Where(s => s.Name == "ABC").ToList();
//traditional way
foreach (var student in Students)
{
if (student.Name == "ABC")
list.Add(student);
}
我认为传统方法更快,因为它只循环一次,从Linq的上面开始,其中where分别对Where方法然后对ToList()方法进行两次迭代.
I think the traditional way is faster, as it loops only once, where as of above of Linq iterates twice once for Where method and then for ToList() method.
我现在正在从事的项目广泛使用了Lists,我发现有很多这样的ToList()和其他方法的使用,如果我采用 list 变量作为 IEnumerable ,然后删除.ToList()并将其进一步用作IEnumerable.
The project I am working on now has extensive use of Lists all over and I see there is alot of such kind of use of ToList() and other Methods that can be made better like above if I take list variable as IEnumerable and remove .ToList() and use it further as IEnumerable.
这些因素是否会对性能产生影响?
Do these things make any impact on performance?
推荐答案
这取决于您的代码.在大多数情况下,使用LINQ确实会降低性能.在某些情况下,此命中点对您可能很重要,但只有在您知道LINQ对您来说太慢时(即,如果对代码进行性能分析表明LINQ是您的代码慢的原因),才应避免使用LINQ.
That depends on your code. Most of the time, using LINQ does cause a small performance hit. In some cases, this hit can be significant for you, but you should avoid LINQ only when you know that it is too slow for you (i.e. if profiling your code showed that LINQ is reason why your code is slow).
但是正确的是,过多地使用ToList()
可能会导致严重的性能问题.仅在必要时才应调用ToList()
.请注意,在某些情况下,添加ToList()
可以大大提高性能(例如,每次迭代从数据库加载集合时).
But you're right that using ToList()
too often can cause significant performance problems. You should call ToList()
only when you have to. Be aware that there are also cases where adding ToList()
can improve performance a lot (e.g. when the collection is loaded from database every time it's iterated).
关于迭代次数:迭代两次"取决于您的确切含义.如果计算在某个集合上调用MoveNext()
的次数,那么可以,使用Where()
会导致迭代两次.操作顺序如下(为简化起见,我将假定所有项目都符合条件):
Regarding the number of iterations: it depends on what exactly do you mean by "iterates twice". If you count the number of times MoveNext()
is called on some collection, then yes, using Where()
this way leads to iterating twice. The sequence of operations goes like this (to simplify, I'm going to assume that all items match the condition):
-
Where()
被调用,目前暂无迭代,Where()
返回一个特殊的可枚举. 调用 -
ToList()
,对从Where()
返回的可枚举调用MoveNext()
. -
Where()
现在在原始集合上调用MoveNext()
并获取值. -
Where()
调用您的谓词,该谓词返回true
. 从 -
MoveNext()
返回,ToList()
获取值并将其添加到列表中. - …
ToList()
调用的Where()
is called, no iteration for now,Where()
returns a special enumerable.ToList()
is called, callingMoveNext()
on the enumerable returned fromWhere()
.Where()
now callsMoveNext()
on the original collection and gets the value.Where()
calls your predicate, which returnstrue
.MoveNext()
called fromToList()
returns,ToList()
gets the value and adds it to the list.- …
这意味着,如果原始集合中的所有 n 个项目都符合条件,则MoveNext()
被称为2 n 次,即 n 从Where()
开始的em>时间和从ToList()
开始的 n 时间.
What this means is that if all n items in the original collection match the condition, MoveNext()
will be called 2n times, n times from Where()
and n times from ToList()
.
这篇关于Linq中的ToList方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!