Skip the last element in an array and return all other elements C#

Go To StackoverFlow.com

3

I would like to return all elements but the last element. I looked at maybe using the Skip() method but got confused. Can anyone help me out please?

Thank you

2012-04-05 19:20
by user1290653
I think you want Take()Jeremy Holovacs 2012-04-05 19:20


9

You can't use Skip() in this case but you must use Take().

var result = list.Take(list.Length-1);
2012-04-05 19:22
by Omar


2

Use Take:

list.Take(list.Length-1);
2012-04-05 19:23
by ionden
THANK YOU!!!! IT WORKED! - user1290653 2012-04-05 19:25


1

You can use Enumerable.Take() for this:

var results = theArray.Take(theArray.Length - 1);

This will let you enumerate all elements except the last in the array.

2012-04-05 19:22
by Reed Copsey


0

You can do the following:

var array = ...;
var arrayExceptLasElement = array.Take(array.Length-1);

Hope that helps!

2012-04-05 19:23
by aKzenT
Ads