Using Linq to select a range of members in a list

Posted by clintp on Stack Overflow See other posts from Stack Overflow or by clintp
Published on 2010-03-08T18:53:14Z Indexed on 2010/03/08 19:06 UTC
Read the original article Hit count: 235

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about c#