Linq: How to check List <bool> for a condition when any of its value == true?

I have a list

List<bool> MyList;
MyList.Add(true);
MyList.Add(false);
MyList.Add(false);

What is a clean way to use linq to check if any value is true? I tried

MyList.Find(SomeBoolean=>SomeBoolean)

but the result is strange.

+5
source share
4 answers

Try:

bool test = MyList.Any(x => x);

But you must initialize your list before inserting anything.

+9
source

Use any

var anyTrue = MyList.Any(i => i);
+4
source

true

List<bool> MyList = new List<bool>();
MyList.Add(true);
MyList.Add(false);
MyList.Add(false);
var listTrue = MyList.Where(c => c);

, Class, .Find, .

var b = MyList.Find(c => c)

, var DataType?

+1

myList - bool

myList = getSelectedChannels(); allTrue = myList.FindAll(a = > a == true);

allTrue bool, (bool - true). allTrue.Count, .

0

All Articles