LINQ扩展方法 - 任何()与凡()与存在() [英] LINQ extension methods - Any() vs. Where() vs. Exists()
问题描述
不幸的是,这些方法的名字让可怕的搜索条件,我一直无法找到一个很好的资源,它解释了这些方法之间的区别 - 在何时使用每个
Unfortunately the names of these methods make terrible search terms, and I've been unable to find a good resource that explains the difference between these methods--as in when to use each.
感谢。
编辑:
排序查询,我试图完全理解是这样的:
The sort of query that I'm trying to fully understand is something like this:
context.Authors.Where(a => a.Books.Any(b => b.BookID == bookID)).ToList();
和感谢所有谁已经回答了。
And thanks to all who've answered.
推荐答案
其中,
返回匹配的predicate项目的新的序列。
Where
returns a new sequence of items matching the predicate.
任何
返回一个布尔值;有一个与一个predicate一个版本(在此情况下它返回任何产品是否不匹配),并没有一个版本(在此情况下,它返回查询那么远是否包含任何项目)。
Any
returns a Boolean value; there's a version with a predicate (in which case it returns whether or not any items match) and a version without (in which case it returns whether the query-so-far contains any items).
我不知道关于存在
- 它不是一个LINQ标准查询操作符。如果有对实体框架的一个版本,也许它基于一个关键的检查存在 - 有几分的特殊形式的任何
? (有一个在的
这是类似存在
方法列表< T> 任何(predicate)
但是predates LINQ)。
I'm not sure about Exists
- it's not a LINQ standard query operator. If there's a version for the Entity Framework, perhaps it checks for existence based on a key - a sort of specialized form of Any
? (There's an Exists
method in List<T>
which is similar to Any(predicate)
but that predates LINQ.)
这篇关于LINQ扩展方法 - 任何()与凡()与存在()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!