c# - lambda 相当于 collection.Count(predicate)

标签 c# linq

什么是以下代码的等价 lambda 表达式

int[] numbers = { 3, 4, 5, 6, 7, 8 };
int count = numbers.Count(x => x > 5 == true); 

我试过这个,但它没有编译:

var c = from number in numbers where number > 5 select numbers.Count;

这个也没有编译:

var c = from number in numbers where number > 5 select new {numbers.Count};

最佳答案

你很接近,只需要像这样将 LINQ 表达式括在括号中:

var c = (from number in numbers where number > 5 select number).Count();

关于c# - lambda 相当于 collection.Count(predicate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16640330/

相关文章:

c# - 用于匹配日志文件中的 APK 文件名的正则表达式模式

c# - 使用 OpenOffice 进行邮件合并

c# - 处理源自线程的错误的标准方法

c# - 在 ActionResult 中使用 ToList ForEach 更新表值

c# - LINQ转SQL

c# - 使用ServiceStack和Signalr的Redis MQ

c# - VS 2013 SDK : How to keybind hotkey only for Code Window Editor?

linq - 为什么我不能在 LinQ 查询中将可为空的 DateTime 转换为字符串?

c# - 使用 LINQ 创建一个包含不同类型的列表?

c# - 将数组转换为字典,值作为项目的索引,键作为项目本身