c# - LINQ如何统计符合条件的元素个数

标签 c# linq count

我已经尝试了很多东西,但对我来说最合乎逻辑的一个似乎是这个:

int divisor = AllMyControls.Take(p => p.IsActiveUserControlChecked).Count();

AllMyControlsUserControls 的集合,我想知道有多少 UserControlsIsActiveUserControlChecked属性设置为 true。

我在 VS 中得到的是:

Cannot convert lambda expression to type 'int' because it is not a delegate type

我的表情有什么问题吗?

最佳答案

int divisor = AllMyControls.Where(p => p.IsActiveUserControlChecked).Count()

或者只是

int divisor = AllMyControls.Count(p => p.IsActiveUserControlChecked);

既然你是初学者,不妨看看Enumerable文档

关于c# - LINQ如何统计符合条件的元素个数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17244039/

相关文章:

c# - linq 从存在任何列表值的查询中删除项目

c# - 读取数据库出错,方法或操作未实现

c# - C#Active Directory添加用户

c# - 多操作系统用户身份验证技术的方法和建议

c# - 如何确定新创建的对象是否具有用户设置的任何属性值

c# - 将 LINQ 序列中的项目发送到返回 void 的方法

c# - 如何对 Entity Framework 进行单元测试?

mysql - 加快临时表查询速度

显示 :none attribute 的 jQuery 计数 div

MySQL:计算员工数量