C# LINQ、Where() 与 FindAll()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1938204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ, Where() vs FindAll()
提问by Grant
Can someone explain how the LINQ functions Where(..) and FindAll(..) differ? They both seem to do the same thing...
有人能解释一下 LINQ 函数 Where(..) 和 FindAll(..) 的区别吗?他们似乎都在做同样的事情......
采纳答案by Adam Robinson
FindAll()
is a function on the List<T>
type, it's not a LINQ extension method like Where
. The LINQ extension methods work on any type that implements IEnumerable
, whereas FindAll
can only be used on List<T>
instances (or instances of classes that inherit from it, of course).
FindAll()
是List<T>
类型上的函数,它不是像Where
. LINQ 扩展方法适用于实现 的任何类型IEnumerable
,而FindAll
只能用于List<T>
实例(或从它继承的类的实例,当然)。
Additionally, they differ in actual purpose. Where
returns an instance of IEnumerable
that is executed on-demand when the object is enumerated. FindAll
returns a new List<T>
that contains the requested elements. FindAll
is more like calling Where(...).ToList()
on an instance of IEnumerable
.
此外,它们在实际用途上有所不同。Where
返回一个IEnumerable
在枚举对象时按需执行的实例。FindAll
返回一个List<T>
包含请求元素的新元素。FindAll
更像是调用Where(...).ToList()
上的一个实例IEnumerable
。
回答by WayneC
If I recall correctly, the main difference (besides what they're implemented on: IEnumerable<T>
vs. List<T>
) is that Where
implements deferred execution, where it doesn't actually do the lookup until you need it -- using it in a foreach loop for example. FindAll
is an immediate execution method.
如果我没记错的话,主要区别(除了它们在:IEnumerable<T>
vs.上实现的内容之外List<T>
)是Where
实现延迟执行,在您需要之前它实际上不会进行查找 - 例如在 foreach 循环中使用它。 FindAll
是一种立即执行的方法。
回答by cfern
The biggest difference to me is that .FindAll is also available in .Net 2.0. I don't always have the luxury to program in .Net 3.5, so I try to remember the 'native' methods of the .Net generic collections.
对我来说最大的不同是 .FindAll 也可以在 .Net 2.0 中使用。我并不总是有幸在 .Net 3.5 中编程,所以我尝试记住 .Net 泛型集合的“本机”方法。
It happened several times that I implemented an already available List method myself because I couldn't LINQ it.
有几次我自己实现了一个已经可用的 List 方法,因为我不能 LINQ 它。
What I find handy in this case is that, using VS2008, I canuse type inference and the lambda syntax. These are compiler features, not framework features. This means I can write this and still remain within .Net 2.0:
在这种情况下,我觉得很方便的是,使用 VS2008,我可以使用类型推断和 lambda 语法。这些是编译器特性,而不是框架特性。这意味着我可以写这个并且仍然保留在 .Net 2.0 中:
var myOddNums = myNums.FindAll(n => n%2==1);
But if you do have LINQ available, keeping the difference between deferred execution and immediate execution is important.
但是,如果您确实有可用的 LINQ,那么保留延迟执行和立即执行之间的区别很重要。
回答by digiben
I did some tests on a list of 80K objects and found that Find()
can be up to 1000% faster than using a Where
with FirstOrDefault()
. I didn't know that until testing a timer before and after each call. Sometimes it was the same time, other times it was faster.
我对一个包含 80K 个对象的列表进行了一些测试,发现它Find()
比使用Where
with快 1000% FirstOrDefault()
。直到在每次通话之前和之后测试计时器之前,我都不知道这一点。有时是相同的时间,有时会更快。