我已经尝试了很多东西,但对我来说最合乎逻辑的一个似乎是这个:
int divisor = AllMyControls.Take(p => p.IsActiveUserControlChecked).Count();
AllMyControls
是 UserControls
的集合,我想知道有多少 UserControls
有 IsActiveUserControlChecked
属性设置为 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/