C# 如何使用 LINQ 计算与条件匹配的元素数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17244039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count the number of elements that match a condition with LINQ
提问by Sturm
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();
AllMyControlsis a Collection of UserControls, what I want to know is how many UserControlshave the IsActiveUserControlCheckedproperty set to true.
AllMyControls是一家集的UserControls,我想知道的是有多少UserControls有IsActiveUserControlChecked属性设置为true。
What I get in VS is:
我在 VS 中得到的是:
Cannot convert lambda expression to type 'int' because it is not a delegate type
What's wrong with my expression?
我的表达有什么问题吗?
采纳答案by Adriano Carneiro
int divisor = AllMyControls.Where(p => p.IsActiveUserControlChecked).Count()
or simply
或者干脆
int divisor = AllMyControls.Count(p => p.IsActiveUserControlChecked);
Since you are a beginner, it would be worthwhile to take a look at Enumerabledocumentation
由于您是初学者,因此值得查看Enumerable文档
回答by bobek
Try
尝试
int divisor = AllMyControls.Where(x => x.IsActiveUserControlChecked == true).Count();
回答by Felipe Oriani
回答by Pierre-Luc Pineault
Why not directly use Count? That == truestatement is also highly redundant.
为什么不直接使用Count?这种== true说法也是非常多余的。
int divisor = AllMyControls.Count(p => p.IsActiveUserControlChecked);
Also, you are getting an error on your Takemethod because it is waiting for an int. You need to specify the number of contiguous elements from the start of the collection you want to get, you cannot put a lambda expression. You need to use TakeWhilefor that. So
此外,您的Take方法出现错误,因为它正在等待int. 您需要从要获取的集合的开头指定连续元素的数量,不能放置 lambda 表达式。为此,您需要使用TakeWhile。所以
int divisor = AllMyControls.TakeWhile(p => p.IsActiveUserControlChecked == true).Count();
would have been correct, but would not work the way you expect it; it stops once the condition is broken. So if AllMyControls contains true, true, false, true, TakeWhilewith Countwill return 2 instead of your expected 3.
本来是正确的,但不会像您期望的那样工作;一旦条件被打破,它就会停止。因此,如果 AllMyControls 包含true, true, false, true,TakeWhilewithCount将返回 2 而不是您预期的 3。
回答by Cemafor
The parameter for Takerequres an intand you are passing in a delegate/ lambda expression. Take is designed to just take the first countof the elements.
的参数Take需要 anint并且您正在传递委托/ lambda 表达式。Take 旨在仅采用第一个count元素。
You can use the Countmethod and pass in a delegate to count the elements that fit its criteria. This way you only iterate the IEnumerable once, rather than first culling out the ones that don't fit your criteria, and then again to actually count them.
您可以使用该Count方法并传入一个委托来计算符合其条件的元素。这样你只迭代 IEnumerable 一次,而不是首先剔除那些不符合你的标准,然后再次实际计算它们。
AllMyControls.Count(p => p.IsActiveUserControlChecked);

