I've just written a couple of pagination extension methods and I was curious to know if there was any improvements I could make.
I'm pretty happy with the base pagination method, where you supply both the page size and page number (as seen below)
public static IEnumerable<T> Paginate<T>(this IEnumerable<T> source, int pageSize, int pageNumber)
{
if (pageSize == 0) throw new ArgumentOutOfRangeException("pageSize");
if (pageNumber == 0) throw new ArgumentOutOfRangeException("pageNumber");
return source.Skip(pageSize * (pageNumber - 1)).Take(pageSize);
}
but I was wondering if there was a better way to do the "auto" pagination, where it returns a IEnumerable<IEnumerable<T>>
public static IEnumerable<IEnumerable<T>> Paginate<T>(this IEnumerable<T> source, int pageSize)
{
source.ThrowIfNull("source");
if (pageSize == 0) throw new ArgumentOutOfRangeException("pageSize");
var pageCount = (int)Math.Ceiling(source.Count() / (double)pageSize);
if (pageSize == 1)
pageCount = source.Count();
for (int i = 1; i <= pageCount; i++)
{
yield return source.Paginate(pageSize, i);
}
}
It seems a bit suspect to have to iterate twice (once for the count and once for the yield return.
Is there any obvious way I could improve these methods?