mvc contrib 寻呼机问题 - AsPagination

发布于 2024-10-10 12:50:24 字数 261 浏览 6 评论 0原文

我可能是错的,但 AsPagination 方法不是效率很低吗,因为它首先从存储库中吸收所有数据来初始化 TotalItems 等?所以不使用分页来使数据访问更加高效。

我还没有找到任何使用存储库和“真正”分页的示例(即在实际 SQL 中使用 TOP 等)。如果我有一个具有以下签名的存储库方法,我该如何使用寻呼机:

IList GetData(int? page, out int TotalItems)

任何反馈将不胜感激。谢谢。

基督教

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋叶绚丽 2024-10-17 12:50:24

您可以使用 CustomPagination类是 MVCContrib 的一部分,如下所示:

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);
}

在您的视图中:

<%= Html.Pager(Model) %>

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) %>
荒芜了季节 2024-10-17 12:50:24

我认为您对分页效率的假设可能会被误导?尝试查看探查器中正在执行的 sql - 它执行两个 sql 语句 - 一个用于检索计数,另一个用于选择前 10 个。下面是从探查器执行的 sql 的结果 -

此 sql 检索获取计数 - 由寻呼机:

SELECT [GroupBy1].[A1] AS [C1]
FROM ( SELECT 
   COUNT(1) AS [A1]
   FROM [dbo].[Customer] AS [Extent1]
)
AS [GroupBy1]

以下是用于检索数据的 sql:

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

注意 TOP(10)、row_number() 以及其中 ... >第二个sql脚本中有20条语句?这有助于澄清吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文