C#实体框架分页 [英] C# Entity Framework Pagination

查看:161
本文介绍了C#实体框架分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让一个复杂的Linq查询的行数以百万计的记录没有击中两次分贝或写入2个独立的查询??

Is there a way to get the row count of a complex Linq query and millions of records without hitting the db twice or writing 2 separate queries??

我可能有我自己的建议。写一个存储过程,但我好与MySQL不MSSQL。

I might have my own suggestion. Write a stored procedure, but I'm good with MySQL not MSSQL.

什么更好的建议将是巨大的。此外,如果任何人都知道,如果微软正在添加此功能的实体框架。

Any better suggestions would be great. Also, if anyone knows if Microsoft is working on adding this feature to the entity framework.

推荐答案

我建议使用取()函数。这可以被用来指定的记录数从LINQ查询或列表取。例如:

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

List<customers> _customers = (from a in db.customers select a).ToList();
var _dataToWebPage = _customers.Take(50);



我用一个MVC应用了类似的技术,其中我写的_customers列表的会话,然后使用当用户点击2页,第3等这样节省多个数据库的命中在此名单进一步分页查询。但是,如果您的列表是非常大的,然后写它太会话可能不是一个好主意。

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.

有关分页可以使用Skip()和Take()函数一起。例如,要获取数据的第2页:

For pagination you can use the Skip() and Take() function together. For example to get page 2 of the data :

var _dataToWebPage = _customers.Skip(50).Take(50);

这篇关于C#实体框架分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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