linq-to-sql - 分页时获取总记录数

标签 linq-to-sql paging

要从数据库中获取页面,我必须执行如下操作:

var cs = ( from x in base.EntityDataContext.Corporates
   select x ).Skip( 10 ).Take( 10 );

这将跳过前 10 行并选择接下来的 10 行。

我怎么知道由于没有分页的查询会导致多少行?我不想运行另一个查询来获取计数。

最佳答案

要在跳过/获取之前获取记录总数,您必须运行单独的查询。获取返回的实际数字将使用 Count(),但如果原始查询已具体化,则不会导致另一个查询。

var q = from x in base.EntityDataContext.Corporates 
        select x;

var total = q.Count();
var cs = q.Skip(10).Take(10);
var numberOnSelectedPage = cs.Count();

关于linq-to-sql - 分页时获取总记录数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2008647/

相关文章:

caching - 即使页面已经在 tlb 中,如何强制页面发生故障?

c# - 在sql 2005中对大表进行分页

swift - CollectionView 自动快速移动到下一个单元格

linq-to-sql - SQL Azure : "The instance of SQL Server you attempted to connect to requires encryption but this machine does not support it"

c# - 关于LINQ to SQL的简单问题

c# - 这里的每个人都在跳上 ORM 潮流吗?

assembly - 跳转到 64 位长模式时出现三重故障

linux - 错误 : unable to handle kernel paging request at

c# - 在 Entity Framework 中按自定义顺序对 VARCHAR 字段进行排序

c# - Linq2Sql -> 根据本地值集合搜索数据库 - "Queries with local collections are not supported"