C# 实体框架分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10145815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Entity Framework Pagination
提问by Jason Foglia
Is there a way to get the row count of a complex Linq query and millions of records withouthitting the db twice or writing 2 separate queries??
有没有办法获得复杂的 Linq 查询的行数和数百万条记录,而无需两次访问数据库或编写 2 个单独的查询?
I might have my own suggestion. Write a stored procedure, but I'm good with MySQL not MSSQL.
我可能有我自己的建议。编写一个存储过程,但我擅长 MySQL 而不是 MSSQL。
Any better suggestions would be great. Also, if anyone knows if Microsoft is working on adding this feature to the entity framework.
任何更好的建议都会很棒。另外,如果有人知道 Microsoft 是否正在努力将此功能添加到实体框架中。
回答by Vladimir Perevalov
The common way to show millions of records is simply not to display all pages. Think of it: if you have millions of records, say 20 or even 100 items per page, then you'll have tens of thousands of pages. It does not make sense to show them all. You can simply load the current page and provide a link to the next page, that's it. Or you may load say 100-500 records, but still show only one page and use the loaded records information to generate page links for first several pages (so know for sure how many next pages are available).
显示数百万条记录的常用方法就是不显示所有页面。想一想:如果您有数百万条记录,比如每页 20 甚至 100 个项目,那么您将拥有数万页。将它们全部展示是没有意义的。您可以简单地加载当前页面并提供指向下一页的链接,就是这样。或者你可以加载 100-500 条记录,但仍然只显示一个页面,并使用加载的记录信息为前几页生成页面链接(所以要确定有多少下一页可用)。
回答by Tim Newton
I'd suggest using the Take() function. This can be used to specify the number of records to take from a linq query or List. For example
我建议使用 Take() 函数。这可用于指定从 linq 查询或列表中获取的记录数。例如
List<customers> _customers = (from a in db.customers select a).ToList();
var _dataToWebPage = _customers.Take(50);
I use a similar technique in an MVC app where I write the _customers list to the session and then use this list for further pagination queries when the user clicks on page 2, 3 etc. This saves multiple database hits. However if your list is very large then writing it too the session is probably not a good idea.
我在 MVC 应用程序中使用了类似的技术,我将 _customers 列表写入会话,然后在用户单击第 2、3 页等时使用此列表进行进一步的分页查询。这可以节省多个数据库命中。但是,如果您的列表非常大,那么也将其写入会话可能不是一个好主意。
For pagination you can use the Skip() and Take() function together. For example to get page 2 of the data :
对于分页,您可以一起使用 Skip() 和 Take() 函数。例如获取数据的第 2 页:
var _dataToWebPage = _customers.Skip(50).Take(50);
回答by S.Mohamed Mahdi Ahmadian zadeh
it is so easy on sql server . you can write this query :
在 sql server 上很容易。你可以写这个查询:
select count() over(), table.* from table
the count () over() will return the count of total row in result , So you don't need to run two query.. Remember that you should run raw sql on your context or use dapper that return result as view model
count () over() 将返回结果中的总行数,因此您不需要运行两个查询.. 请记住,您应该在上下文中运行原始 sql 或使用将结果作为视图模型返回的 dapper
回答by jk1990
I was recently inspired by (copied from) this Code Project article Entity Framework pagination
我最近受到(复制自)这篇代码项目文章实体框架分页的启发
The code:
编码:
public async Task<IList<SavedSearch>> FindAllSavedSearches(int page, int limit)
{
if (page == 0)
page = 1;
if (limit == 0)
limit = int.MaxValue;
var skip = (page - 1) * limit;
var savedSearches = _databaseContext.SavedSearches.Skip(skip).Take(limit).Include(x => x.Parameters);
return await savedSearches.ToArrayAsync();
}
I'm not experienced with Entity Framework and I've not tested it for performance, so use with caution :)
我对实体框架没有经验,也没有测试过它的性能,所以请谨慎使用 :)
回答by Chitova263
If you need a quick solution you can use XPagedList https://github.com/dncuug/X.PagedList. XPagedList is a library that enables you to easily take an IEnumerable/IQueryable, chop it up into "pages", and grab a specific "page" by an index. For example
如果您需要快速解决方案,您可以使用 XPagedList https://github.com/dncuug/X.PagedList。XPagedList 是一个库,可让您轻松获取 IEnumerable/IQueryable,将其分成“页面”,并通过索引抓取特定的“页面”。例如
var products = await _context.Products.ToPagedListAsync(pageNumber, pageSize)

