Question

i would like to iterate over the items of a List<T>, except the first, preserving the order. Is there an elegant way to do it with LINQ using a statement like:

foreach (var item in list.Skip(1).TakeTheRest()) {....

I played around with TakeWhile , but was not successful. Probably there is also another, simple way of doing it?

Was it helpful?

Solution

From the documentation for Skip:

Bypasses a specified number of elements in a sequence and then returns the remaining elements.

So you just need this:

foreach (var item in list.Skip(1))

OTHER TIPS

Just do:

foreach (var item in input.Skip(1))

There's some more info on the MSDN and a simple example that's downloadable here

Wouldn't it be...

foreach (var in list.Skip(1).AsEnumerable())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top