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

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

问题描述

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



我这样做this:

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

是否有更短的方式像 int c = ListfilesToProcess.count是的,使用LINQ的

计数
方法,重载使用谓词

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

一般来说,每当你想要摆脱一个循环应该看看LINQ。


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.

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);

解决方案

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

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.

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

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