c# - Generic List, items counting with conditional-statement -
i have generic list. has listfilestoprocess.count property returns total number of items, want count number of items in list conditional-statement.
i doing this:
int c = 0; foreach (filestoprocessdatamodels item in listfilestoprocess) { if (item.ischecked == true) c++; } is there shorter way int c = listfilestoprocess.count(item => item.ischecked == true);
yes, use linq's count method, overload taking predicate:
int count = listfilestoprocess.count(item => item.ischecked); in general, whenever feel want rid of loop (or simplify it) - should @ linq.
Comments
Post a Comment