tags:

views:

54

answers:

4

How do I retrieve all elements after the first one not starting with a "-" using linq?

var arr = new[] {"-s1", "-s2", "va", "-s3", "va2", "va3"};
var allElementsAfterVA = from a in arr where ???? select a;

I want allElementsAfterVA to be "-s3", "va2", "va3"

+5  A: 

To find all of the arguments after the first that does NOT start with "-", you can do:

var elementsAfterFirstNonDash = arr.SkipWhile(i => i[0] != '-').Skip(1);

This finds "va", then skips it via Skip(1). The rest of the arguments will be returned.

Reed Copsey
That seems like a solution only for this test case. Does not seem like you can use this on any data set...
astander
It still seems like a test data specific solution. Maybe something a little more generic. Or is the question to vague to create a generic solution?
astander
Yes, the solution seems to only be for this test case. As an example, I want to get all elements after the first one *not* starting with "-"...
MatteS
I aggree with the last solution. Seems good. +1
astander
@MatteS: I just rewrote this after your question edit. I believe it's exactly what you want, now.
Reed Copsey
@MatteS. Next time, please be more specific as to what you want, as the original question made it very difficult. **Good answer @Reed**
astander
@MatteS: Does this do what you're after now?
Reed Copsey
A: 

Can you please be more clear? If I understood correctly, the first one starting with a "-" is "-s1", so the elements after this would be "-s2", "va", "-s3", "va2", "va3" and not "-s3", "va2", "va3"

geeth
Sorry, I missed a *not* there... I meant to say "after the first one not starting with a "-""..
MatteS
A: 

I don't quite get the question from your text, but looking at the example: Did you look at SkipWhile()? Seems to be related/useful?

Benjamin Podszun
A: 
arr.Where((n, i) => i > 0 && n.StartsWith("-"))

yields

-s2 -s3

Is this what you meant?

mmsmatt