Skip to content

Instantly share code, notes, and snippets.

@xrisdoc
Created June 17, 2020 16:07
Show Gist options
  • Save xrisdoc/3a279d64bedcd7bd5d1a3c93ef8b3552 to your computer and use it in GitHub Desktop.
Save xrisdoc/3a279d64bedcd7bd5d1a3c93ef8b3552 to your computer and use it in GitHub Desktop.
Splits an IEnumerable object into multiple IEnumerable objects
private IEnumerable<IEnumerable<T>> SplitIntoPages<T>(IEnumerable<T> items, int itemsPerPage)
{
var pages = new List<List<T>>();
int pageCount = (int)Math.Ceiling((items.Count() / (decimal)itemsPerPage));
int skipCount = 0;
for (int i = 0; i < pageCount; i++)
{
var subset = items.Skip(skipCount).Take(itemsPerPage).ToList();
pages.Add(subset);
skipCount += itemsPerPage;
}
return pages;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment