c# array select
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 ne开发者_如何学Pythonw 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();
bool[] result = a.Where(x => x).ToArray();
精彩评论