通用列表,使用条件语句计数的项目 [英] Generic List, items counting with conditional-statement

查看:25
本文介绍了通用列表,使用条件语句计数的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用列表.它有一个 ListfilesToProcess.Count 属性,它返回项目总数,但我想用条件语句计算列表中的特定项目数.

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.

我是这样做的:

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

有没有更短的方法,比如int c = ListfilesToProcess.count(item => item.IsChecked == true);

Is there any shorter way like int c = ListfilesToProcess.count(item => item.IsChecked == true);

推荐答案

是的,使用 LINQ 的 Count 方法,以及 重载取谓词:

Yes, use LINQ's Count method, with the overload taking a predicate:

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

一般来说,每当您想摆脱循环(或简化它)时,您都应该查看 LINQ.

In general, whenever you feel you want to get rid of a loop (or simplify it) - you should look at LINQ.

这篇关于通用列表,使用条件语句计数的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆