Deadly Nicotine Deadly Nicotine - 1 month ago 11
C# Question

Does LINQ have any easy/elegant way to take the first element and put it at the end?

Or should I say, skip the first element and come back to it at the end.

e.g. say I have

int[] arr = { 2, 4, 3, 9, 1, 0 };


and want to iterate through it like

{ 4, 3, 9, 1, 0, 2 }


I know one way would be like

foreach(int i in arr.Skip(1).Append(new int[] { arr.First() }))


which is why I'm asking whether there's a better looking and/or more efficient way.

Answer

Only slightly cleaner than what you've got:

foreach(int i in arr.Skip(1).Concat(arr.Take(1)))
{
    ...
}