FundraisingMenu.SearchServices.FundraisingMenuSearchServices.PageResults C# (CSharp) Method

PageResults() private static method

We need to use a more advanced paging strategy because there are virtual records that will not be displayed in the table. So instead of using the start and length arguments from the query as a hard Skip/Take boundary, we need to count how many virtual records preceeded the current page and we need to take records up to the query length, but excluding virtual records from the count.
private static PageResults ( DataTableParameters query, List data ) : List
query FundraisingMenu.Integration.DataTableParameters
data List
return List
        private static List<FundraisingMenuResult> PageResults(DataTableParameters query, List<FundraisingMenuResult> data)
        {
            // Count how many "virtual" records there are before the page begins
            int virtualRecordsBeforeStart = data.Take(query.start).Where(r => r.HubId == null).Count();
            // That's our starting point.
            IEnumerable<FundraisingMenuResult> recordsFromStart = data.Skip(query.start + virtualRecordsBeforeStart);

            // Take an initial set of records using the page size.
            IEnumerable<FundraisingMenuResult> pagedResult = recordsFromStart.Take(query.length);

            // Take more records until we have query.length real records or we hit the end of the list.
            int totalLength = recordsFromStart.Count();
            int currentCount = pagedResult.Count();
            int virtualCount = pagedResult.Where(r => r.HubId == null).Count();
            while (currentCount - virtualCount < query.length && currentCount + virtualCount < totalLength)
            {
                pagedResult = recordsFromStart.Take(currentCount + virtualCount);
                currentCount = pagedResult.Count();
                virtualCount = pagedResult.Where(r => r.HubId == null).Count();
            }

            return pagedResult.ToList();
        }