Any()
以下查询在做什么?
context.Customers
.Include("InternetSales")
.Where(c => c.InternetSales.Any())
.Take(100);
你将如何用简单的英语读出这个查询?例如,以下内容是否准确?
“通过相关的 100 次互联网销售获得客户。”
(我知道代码中没有“get”,但你明白我的意思。)
运算符检查某个可Any
枚举/集合是否包含至少一项,即它是否为非空。
所以我猜你的查询可以写成:
“前 100 位至少进行过一次网络销售的客户”
或者,更接近金属:
“前 100 个
Customer
具有非空集合的对象InternetSales
”
.Any()
is similar to .Count() > 0
, but it will consume at most one item in the collection, while Count
consumes the complete collection, so Any
is generally more efficient and works for infinite sequences, too. Provided you're not interested in the exact number of items, Any
also expresses the intent of checking for non-emptiness more clearly.