Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

counting integers in a list

Tags:

c#

linq

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 = trueand return the result as an integer?

like image 223
user547794 Avatar asked Feb 20 '23 00:02

user547794


2 Answers

Use Count

var count = list.Count(item => item.NoNotEncounterBarriersResult)

http://msdn.microsoft.com/en-us/library/bb535181.aspx

like image 127
Ian Newson Avatar answered Feb 27 '23 05:02

Ian Newson


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.

like image 28
Mark Byers Avatar answered Feb 27 '23 07:02

Mark Byers