How to check a value in a logical list?

I have this list: List<bool> values = new List<bool>();

Filled out: True,True,False,True,False,False,True

When I do this: int amountTrue = values.Count(v => true); it returns 7. This is just the number of values ​​in the List. I think it checks to see if a value exists, but that is not what I want.

How to get the number of Truevalues ​​in a List using Countor any other chaining method? I know that I can go through it, but I think it can be done easier.

+4
source share
5 answers

Count , , . Count , "" . , , " , , true ", , , . , , , , :

values.Count(v => v);

longhand

values.Where(v => v).Count();
+10
List<bool> values = new List<bool>() { true, true, false, true, false, false, true };

Console.WriteLine(values.Count(v => v == true)); //output : 4
Console.WriteLine(values.Count(v => v == false)); //output : 3

//equivalent
Console.WriteLine(values.Count(v => v)); //v == true, output : 4
Console.WriteLine(values.Count(v => !v)); //v == false, output : 3

:

4

3

4

3

+4

, , , . , , , , .

:

int nbTrue = 0; // choose a significant name
int nbFalse = 0; // choose a significant name

nbTrue++;
nbFalse++;
0

var count = values.Count(v = > true == v)

0

values.Count(v = > v); v values.Count(v = > true); true.

0

Source: https://habr.com/ru/post/1606665/


All Articles