The SkipLast Extension Method

When writing queries, just as you sometimes want to skip the first n items in a collection, on occasion you want to skip the last n items.  You could certain count the items remaining in the collection, and use the Take operator to take all but the last, but this would mean iterating the collection twice, and would result in uglier code.  This post presents the SkipLast Extension method, which allows you to skip the last n items.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOCIn the next post, I’m going to present an enhancement to LtxOpenXml classes, which use LINQ to XML to query Open XML documents, to allow you to write queries on tables within spreadsheet (XLSX) documents.  I needed SkipLast to implement this.

Here is the implementation of the extension method, and a couple of examples of its use:

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;

publicstaticclassMyExtensions
{
publicstaticIEnumerable<T> SkipLast<T>(thisIEnumerable<T> source,
int count)
{
Queue<T> saveList = newQueue<T>();
int saved = 0;
foreach (T item in source)
{
if (saved < count)
{
saveList.Enqueue(item);
++saved;
continue;
}
saveList.Enqueue(item);
yieldreturn saveList.Dequeue();
}
yieldbreak;
}
}

classProgram
{
staticvoid Main(string[] args)
{
int[] a = new[] { 1, 2, 3, 4, 5 };
var b = a.SkipLast(2);
foreach (var item in b)
Console.WriteLine(item);

List<string> c = newList<string>() {
"one", "two", "three", "four", "five"
};
var d = c.Skip(1).SkipLast(1);
foreach (var e in d)
Console.WriteLine(e);
}
}

This example outputs:

1
2
3
two
three
four

Code is attached.

SkipLast.cs