I have a bool array
a[1] = true
a[2] = false
a[3] = true
a[4] = true
how do i select only true values to a new array?
Thanks!
I don't really know why you would want to do this but...
bool[] a = {true, false, true, true};
bool[] b = a.Where(x => x).ToArray();
If you just want to count how many "true"s there are:
int c = a.Count(x => x);
If you mean a new array containing the indices of 'a' that had a value of true...
// Assuming here that a begins at 0, unlike your example...
Enumerable.Range(0, a.Length).Where(i=>a[i]).ToArray();
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