how to take all array elements except last element in C#

var remStrings = queries.Take(queries.Length - 1);

No need to Reverse and Skip. Just take one less element than there are in the array.

If you really wanted the elements in the reverse order, you could tack on a .Reverse() to the end.


Microsoft's Reactive Extensions' Team has the Interactive Extensions (NuGet "System.Interactive") that lets you do this:

var remStrings = queries.SkipLast(1);

Why not just have:

var remStrings = queries.Take(queries.Length-1);

Which will return them in the same order.

Append .Reverse() to swap the order if that's a necessary requirement:

var remStrings = queries.Take(queries.Length-1).Reverse();

Tags:

C#

Arrays