c# - 在此 LINQ 查询中 Any() 是什么意思?

标签 c# linq any

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-empty InternetSales collection"

.Any() 类似于.Count() > 0,但它最多会消耗集合中的一项,而Count 使用完整的集合,因此 Any 通常更高效并且也适用于无限序列。如果您对项目的确切数量不感兴趣,Any 也更清楚地表达了检查非空的意图。

关于c# - 在此 LINQ 查询中 Any() 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10773048/

相关文章:

c# - 如何在 C# 或 Linq 中比较大于或小于运算符值的两个字节数组?

python - 如何形成 'any' 条件的相反语句?

c# - Coroutine中的 "yield return 0"和 "yield return null"有什么区别?

c# - 接受文件到/从 C# WebService 返回文件

c# - 控制台应用程序/服务需要 40 秒才能启动

python - 在 python 文件中使用 google.protobuf.Any

python - 在数据框列中查找字典的值并修改它

c# - 使用字符串调用特定的命名空间和类

c# - 在 Linq 中使用匿名类型加入 2 个空字符串列表

c# - Enumerable.Aggregate 错误