简体   繁体   English

通用列表,使用条件语句计数的项目

[英]Generic List, items counting with conditional-statement

I have a Generic List. 我有一个通用列表。 it has a ListfilesToProcess.Count property which returns total number of items, but I want to count certain number of items in list with conditional-statement. 它有一个ListfilesToProcess.Count属性,它返回项目的总数,但我想用条件语句计算列表中的某些项目数。

I am doing it like this: 我是这样做的:

int c = 0;
foreach (FilesToProcessDataModels item in ListfilesToProcess)
            {
                if (item.IsChecked == true)
                    c++;
            }

Is there any shorter way like int c = ListfilesToProcess.count(item => item.IsChecked == true); 是否有任何更短的方式,如int c = ListfilesToProcess.count(item => item.IsChecked == true);

Yes, use LINQ's Count method, with the overload taking a predicate : 是的,使用LINQ的Count方法, 过载采用谓词

int count = ListFilesToProcess.Count(item => item.IsChecked);

In general, whenever you feel you want to get rid of a loop (or simplify it) - you should look at LINQ. 一般来说,每当你想要摆脱循环(或简化它)时,你应该看看LINQ。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM