本文介绍了如何计算与LINQ匹配条件的元素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了很多事情,但是对我来说,最合乎逻辑的事情似乎是这样:
I've tried a lot of things but the most logical one for me seems this one:
int divisor = AllMyControls.Take(p => p.IsActiveUserControlChecked).Count();
AllMyControls
是UserControls
的集合,我想知道有多少UserControls
的IsActiveUserControlChecked
属性设置为true.
AllMyControls
is a Collection of UserControls
, what I want to know is how many UserControls
have the IsActiveUserControlChecked
property set to 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
文档
Since you are a beginner, it would be worthwhile to take a look at Enumerable
documentation
这篇关于如何计算与LINQ匹配条件的元素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!