mvc contrib pager question - AsPagination
-
30-09-2019 - |
Question
I might be wrong but is the AsPagination method not very inefficient as it sucks all the data from a repository first to initialise TotalItems etc.? So paging is not used to make the data access more efficient.
I have not found any examples which use a repository and 'true' paging (i.e. uses TOP etc. in the atcual SQL). How do I use the Pager if I have a repository method with this signature:
IList GetData(int? page, out int TotalItems)
Any feedback would be very much appreciated. Thanks.
Christian
Solution
You could use the CustomPagination<T>
class which is part of MVCContrib like this:
public ActionResult Index(int page)
{
int totalItems;
int pageSize = 10;
var data = Repository.GetData(page, out totalItems);
var paginatedData = new CustomPagination<Bla>(
data, page, pageSize, totalItems
);
return View(paginatedData);
}
and inside your view:
<%= Html.Pager(Model) %>
OTHER TIPS
I think you may be misdirected with your assumptions on the paging efficiency? Try taking a look at the executing sql in profiler - it executes two sql statements - one to retrieve the count and one to select the top 10. Below is the results of the executed sql from profiler -
This sql retrieves gets the count - used by the pager:
SELECT [GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[Customer] AS [Extent1]
)
AS [GroupBy1]
While the following is the sql used to retrieve the data:
SELECT TOP (10)
[Extent1].[CustomerId] AS [CustomerId],
[Extent1].[FirstName] AS [FirstName],
[Extent1].[LastName] AS [LastName],
...
FROM ( SELECT [Extent1].[CustomerId] AS [CustomerId], [Extent1].[FirstName] AS [FirstName], [Extent1].[LastName] AS [LastName], ..., row_number() OVER (ORDER BY [Extent1].[Company] ASC) AS [row_number]
FROM [dbo].[Customer] AS [Extent1]
) AS [Extent1]
WHERE [Extent1].[row_number] > 20
ORDER BY [Extent1].[Company] ASC
Notice the TOP(10), row_number() over, and where ... > 20 statements in the second sql script? Does that help clarify?