LINQ: How to skip one then take the rest of a sequence

C#LinqListLoopsSkip

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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))

Solution 2 - C#

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

Solution 3 - C#

Wouldn't it be...

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcelView Question on Stackoverflow
Solution 1 - C#Mark ByersView Answer on Stackoverflow
Solution 2 - C#ChrisFView Answer on Stackoverflow
Solution 3 - C#TomTomView Answer on Stackoverflow