Generic List, items counting with conditional-statement
11,108
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.
Author by
Zeeshanef
Updated on June 07, 2022Comments
-
Zeeshanef almost 2 years
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);
-
nam over 6 yearsYour response helped me do the following:
foreach(var item in Model.listOfObjects(t => t.ObjType == "M")
. I'm using it in anMVC View
where I'm displaying two separate HTML tables - one for male ( ObjType = "M") students and one for female (ObjType = "F")