Skip to content

Instantly share code, notes, and snippets.

@SaxxonPike
Created March 25, 2016 18:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SaxxonPike/d941ecdc23ed79a31f55 to your computer and use it in GitHub Desktop.
Save SaxxonPike/d941ecdc23ed79a31f55 to your computer and use it in GitHub Desktop.
C# forward pagination extension method
using System.Collections.Generic;
namespace Extensions
{
public static class EnumerablePaginationExtensions
{
public static IEnumerable<IEnumerable<T>> Paginate<T>(this IEnumerable<T> items, int pageSize)
{
var page = new List<T>();
foreach (var item in items)
{
page.Add(item);
if (page.Count >= pageSize)
{
yield return page;
page = new List<T>();
}
}
if (page.Count > 0)
{
yield return page;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment