SkipLast

    public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source)
    {
    	if (!source.Any())
    	{
    		yield break;
    	}
    	Queue<T> items = new Queue<T>();
    	items.Enqueue(source.First());
    	foreach(T item in source.Skip(1))
    	{
    		yield return items.Dequeue();
    		items.Enqueue(item);
    	}
    }
Example:
List<int> items = new List<int>(new[]{ 1, 2, 3, 4, 5, 6});
foreach(int item in items.SkipLast())
{
  Console.WriteLine(item);
}

gets:
1
2
3
4
5

Description

take all but the last item from an IEnumerable<T>

Details

Double click on the code to select all.

 

;