Any()
在下面的查询中做了什么?
context.Customers
.Include("InternetSales")
.Where(c => c.InternetSales.Any())
.Take(100);
你会如何用简单的英语读出这个查询?例如,以下是否准确?
"Get customers with their associated 100 internet sales."
(我知道代码中没有“get”,但你明白我的意思。)
最佳答案
Any
运算符检查某些可枚举/集合是否包含至少一项,即它是否非空。
所以我想您的查询可以读作:
"the first 100 customers that have made at least one internet sale"
或者,更接近于金属:
"the first 100
Customer
objects that have a non-emptyInternetSales
collection"
.Any()
类似于.Count() > 0
,但它最多会消耗集合中的一项,而Count
使用完整的集合,因此 Any
通常更高效并且也适用于无限序列。如果您对项目的确切数量不感兴趣,Any
也更清楚地表达了检查非空的意图。
关于c# - 在此 LINQ 查询中 Any() 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10773048/