与PagedList分页,它是有效的? [英] Paging with PagedList, is it efficient?

查看:168
本文介绍了与PagedList分页,它是有效的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图相当长一段时间做寻呼,现在我发现这个教程分页与MVC:

I have been trying to do Paging for quite a while now and I found this tutorial for paging with MVC:

ASP.NET MVC分页完全可以做到的

现在在这个解决方案,我查询数据库的整组客户,然后我回到客户,而不是一个正常的列表的pagedlist。

Now, in this solution, I query the DB for the entire set of clients and then I return a pagedlist of clients instead of a normal list.

我觉得这是令人不安的,因为我只打算每页显示10个或20个条目,而我的数据库很容易有超过他们一百万。因此,我想说明的索引页,每次查询整个数据库,似乎充其量是一个糟糕的解决方案。

I find this disturbing, because I only plan to show 10 or 20 entries per page, and my DB will easily have over a million on them. Thus, querying the entire DB each time I want to show the Index page, seems to be a poor solution at best.

如果我理解不对的地方,请随时要砍我的权利,但对于我这种解决方案是什么,但完美的。

If I am understanding something wrong, please feel free to cut me right now, but for me this solution is anything but perfect.

让我missunderstood的东西吗?
有没有更有效的解决方案或库在那里与MVC分页?

Have I missunderstood something ? Is there a more efficient solution or library out there for pagination with MVC?

推荐答案

当然分页需要的知识总的结果,以计数的逻辑来确定多少页有等,但不是打倒所有的结果只是建立你的数据库查询返回分页金额(如30),以及所有的计数结果。

Naturally paging will require knowledge of the total result count in order for the logic to determine how many pages there are etc. However instead of bringing down all the results just build your query to the Database to return the paged amount (e.g 30) and as well as the count of all the results.

例如,如果您使用实体框架,或LINQ2SQL你可以做这样的事情。

For example, if you were using Entity Framework, or LINQ2SQL you could do something like this

IQueryable<Result> allResults = MyRepository.RetrieveAll();

var resultGroup = allResults.OrderByDescending(r => r.DatePosted)
                                               .Skip(60)
                                               .Take(30)
                                               .GroupBy(p => new {Total = allResults.Count()})
                                               .First();

var results = new ResultObject
{
    ResultCount = resultGroup.Key.Total,
    Results = resultGrouping.Select(r => r)
};

由于我们还没有对我们的结果集做了.ToList(),直到我们已经完成了我们想要的,我们没带,结果到内存中。当我们在我们的结果集调用。首先()这样做。

Because we haven't done a .ToList() on our result set until we have finalised what we want, we haven't brought the results into memory. This is done when we call the .First() on our result set.

最后,可以用我们的对象,我们结束了(ResultObject)来然后做分页稍后的。因为我们有伯爵,我们已经知道我们是在(3,我们跳过60,30每页)的页面,我们有显示的结果。

Finally our Object that we end up with (ResultObject) can be used to then do the paging later on. As we have the count, we already know what page we are on (3 as we skipped 60, with 30 per page) and we have the results to display.

< STRONG>的延伸阅读和信息

Further Reading and Information

如何:翻阅查询结果

服务器端分页与实体框架

这篇关于与PagedList分页,它是有效的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆