I have a collection of integers in a list. I know I can do something like this to get a specific occurence:
List<ResultsViewModel> list = data.ToList<ResultsViewModel>();
Response.Write(list[2].NoNotEncounterBarriersResult);
But how can I loop through and count number of instances of list[i].NoNotEncounterBarriersResult = true
and return the result as an integer?
Use Count
var count = list.Count(item => item.NoNotEncounterBarriersResult)
http://msdn.microsoft.com/en-us/library/bb535181.aspx
Use Count
:
int count = list.Count(x => x.NoNotEncounterBarriersResult);
From the documentation:
Returns a number that represents how many elements in the specified sequence satisfy a condition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With