LINQ、Where() 与 FindAll() [英] LINQ, Where() vs FindAll()
问题描述
有人能解释一下 LINQ 函数 Where(..) 和 FindAll(..) 的区别吗?他们似乎都在做同样的事情......
Can someone explain how the LINQ functions Where(..) and FindAll(..) differ? They both seem to do the same thing...
推荐答案
FindAll()
是 List
类型的函数,不是 LINQ 扩展Where
之类的方法.LINQ 扩展方法适用于任何实现 IEnumerable
的类型,而 FindAll
只能用于 List
实例(或类的实例当然是从它继承的).
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).
此外,它们在实际用途上有所不同.Where
返回一个 IEnumerable
的实例,该实例在枚举对象时按需执行.FindAll
返回一个包含所请求元素的新 List
.FindAll
更像是在 IEnumerable
的实例上调用 Where(...).ToList()
.
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
.
这篇关于LINQ、Where() 与 FindAll()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!