score:36

Accepted answer

you could do something like this:

ienumerable<person> persons = ..

var firstpersonafterjack = persons.skipwhile(p => p.name != "jack")
                                  .elementat(1); //zero-indexed, means second

the idea is to produce a sequence resulting in skipping elements until you meet the condition, then take the second element of that sequence.

if there's no guarantee that the query will return a result (e.g. a match is never found, or is the last element of the sequence), you could replace elementat with elementatordefault, and then do a null-test to check for success / failure.

i notice you say in your question that you have an ordered list of people. if you could explain what that means in more detail, we might be able to provide a better answer (for example, we may not have to linear-search the sequence).

score:0

you can use code like this:

string todir = environment.getcommandlineargs().skipwhile(x => x != "/to").skip(1).take(1).firstordefault();

this value gets == null if "/to" command line argument not given, non-null if path was provided.

score:5

skipwhile is a method that takes a predicate and skips everything until the predicate is false. it returns that element and everything after.

var remainingpeople = collectionofpeople.skipwhile(p => !istheperson(p));
if (remainingpeople.count() == 1)
{
    // the person was the last in the list.
}
var nextperson = remainingpeople.skip(1).first();

where istheperson is a method that takes a person and returns true if it is the person you are interested it.


Related Query

More Query from same tag