Skip to content

Instantly share code, notes, and snippets.

@deepumi
Created October 10, 2019 12:13
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 deepumi/14526861955a1abc1b2b3c45a77f77f2 to your computer and use it in GitHub Desktop.
Save deepumi/14526861955a1abc1b2b3c45a77f77f2 to your computer and use it in GitHub Desktop.
Alternative to C# LINQ Skip() & Take() for pagination objects
internal static class FeedPagerService
{
private const int PageSize = 10;
internal static (List<FeedItem> feeds, int pageSize, int maxRecords) SkipRecords(int pageIndex, List<FeedItem> feeds)
{
if (pageIndex < 0) pageIndex = 0; //fix negative
var startPage = pageIndex <= 1 ? 0 : (pageIndex - 1) * PageSize;
int endPage;
if (pageIndex <= 1)
{
endPage = 10;
}
else
{
endPage = PageSize * pageIndex;
}
var maxRecords = feeds.Count;
var resultFeed = new List<FeedItem>(10);
for (var i = startPage; i < endPage; i++)
{
if (i < maxRecords) resultFeed.Add(feeds[i]);
}
return (resultFeed, PageSize, maxRecords);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment