gpt4 book ai didi

c# - PagedList 不会在 asp.net mvc 中进行真正的分页

转载 作者:行者123 更新时间:2023-12-03 03:38:34 25 4
gpt4 key购买 nike

在我的项目中,我需要对查询进行分页。我使用 Entity Framework 从数据库获取数据。 This video on youtube解释如何对查询进行分页。简而言之,视频说您所要做的就是将 PagedList.Mvc 安装到您的项目和此代码

.ToPagedList(PageNumber,PageSize)

首先,这似乎对我有用,但我发现(在使用 Sql server profile 检查 Entity Framework 传递给 sql server 的内容之后) Entity Framework 向 sql server 询问所有数据,然后选择需要的数据。更清楚地说,这是我在 C#(服务器端)上的代码

var messages = db.Messages.Where(x => x.User1 == 1 && x.User2 == 2).OrderBy(x=>x.Date).ToList().ToPagedList(1,4);

传递给sql server的是这样的:

exec sp_executesql N'SELECT 
[Project1].[Id] AS [Id],
[Project1].[User1] AS [User1],
[Project1].[User2] AS [User2],
[Project1].[Direction] AS [Direction],
[Project1].[TextContent] AS [TextContent],
[Project1].[ReadBit] AS [ReadBit],
[Project1].[Date] AS [Date],
[Project1].[ReceivedBit] AS [ReceivedBit]
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Extent1].[User1] AS [User1],
[Extent1].[User2] AS [User2],
[Extent1].[Direction] AS [Direction],
[Extent1].[TextContent] AS [TextContent],
[Extent1].[ReadBit] AS [ReadBit],
[Extent1].[ReceivedBit] AS [ReceivedBit],
[Extent1].[Date] AS [Date]
FROM [Messages].[Messages] AS [Extent1]
WHERE ([Extent1].[User1] = @p__linq__0) AND ([Extent1].[User2] = @p__linq__1)
) AS [Project1]
ORDER BY [Project1].[Date] ASC',N'@p__linq__0 int,@p__linq__1 int',@p__linq__0=1,@p__linq__1=2

所以sql server中没有分页,我认为这样的分页没有任何优势。我是否错过了一些无法使其发挥作用的东西?或者就是这样工作?

最佳答案

PagedList附加Skip()Take()到您的查询(计算从 PageNumberPageSize 跳过的行数)。但查询应该是原来的IQueryable这些添加内容会到达查询提供程序并影响 SQL 转换。 ToList断开此连接。

关于c# - PagedList 不会在 asp.net mvc 中进行真正的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33037210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com