Created
October 10, 2019 12:13
-
-
Save deepumi/14526861955a1abc1b2b3c45a77f77f2 to your computer and use it in GitHub Desktop.
Alternative to C# LINQ Skip() & Take() for pagination objects
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
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