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

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -