Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonidy/a2f7b37b4f6d8c6c94cbba2961c01b21 to your computer and use it in GitHub Desktop.
Save tonidy/a2f7b37b4f6d8c6c94cbba2961c01b21 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