Using Linq to select a range of members in a list
- by clintp
Given a list of elements like so:
int[] ia = new int[] { -4, 10, 11, 12, 13, -1, 9, 8, 7, 6, 5, 4, -2,
6, 15, 32, -5, 6, 19, 22 };
Is there an easy way in Linq to do something along the lines of "Select the elements from the -1 up to the next negative number (or the list exhausts)"? A successful result for -1 would be (-1, 9, 8, 7, 6, 5, 4). Using -2 would give the result (-2, 6, 15, 32).
Not a homework problem. I'm just looking at an implementation using a bool, a for loop, and an if wondering if there's a cleaner way to do it.