Skip to content

Commit

Permalink
Removed unnecessary data iteration. (#916)
Browse files Browse the repository at this point in the history
GridDataProvider iterated twice over data source when paging is active - this has been optimized.

Co-authored-by: Neovex <[email protected]>
  • Loading branch information
Neovex and Neovex authored Oct 26, 2024
1 parent 50fa067 commit a618c68
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions blazorbootstrap/Models/GridDataProviderRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public GridDataProviderResult<TItem> ApplyTo(IEnumerable<TItem> data)
if (index == 1)
{
orderedData = sortItem.SortDirection == SortDirection.Ascending
? resultData.OrderBy(sortItem.SortKeySelector.Compile())
: resultData.OrderByDescending(sortItem.SortKeySelector.Compile());
? resultData.OrderBy(sortItem.SortKeySelector.Compile())
: resultData.OrderByDescending(sortItem.SortKeySelector.Compile());
}
else
{
if (orderedData != null)
orderedData = sortItem.SortDirection == SortDirection.Ascending
? orderedData.ThenBy(sortItem.SortKeySelector.Compile())
: orderedData.ThenByDescending(sortItem.SortKeySelector.Compile());
? orderedData.ThenBy(sortItem.SortKeySelector.Compile())
: orderedData.ThenByDescending(sortItem.SortKeySelector.Compile());
}

index++;
Expand All @@ -61,15 +61,11 @@ public GridDataProviderResult<TItem> ApplyTo(IEnumerable<TItem> data)
}

// apply paging
var skip = 0;
var take = data.Count();
var totalCount = resultData!.Count(); // before paging

if (PageNumber > 0 && PageSize > 0)
{
skip = (PageNumber - 1) * PageSize;
take = PageSize;
resultData = resultData!.Skip(skip).Take(take);
int skip = (PageNumber - 1) * PageSize;
resultData = resultData!.Skip(skip).Take(PageSize);
}

return new GridDataProviderResult<TItem> { Data = resultData, TotalCount = totalCount };
Expand Down

0 comments on commit a618c68

Please sign in to comment.