Created
March 25, 2016 18:43
-
-
Save SaxxonPike/d941ecdc23ed79a31f55 to your computer and use it in GitHub Desktop.
C# forward pagination extension method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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