获取分页时的记录总数 [英] Get the total number of records when doing pagination

查看:47
本文介绍了获取分页时的记录总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

To get a page from a database I have to execute something like this:

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

这将跳过前10行,并选择下10行.

This will skip the first 10 rows and will select the next 10.

如何知道没有分页的查询会导致多少行?我不想运行另一个查询来获取计数.

How can I know how many rows would result because of the query without pagination? I do not want to run another query to get the count.

推荐答案

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

To get the total number of records before skip/take you have to run a separate query. Getting the actual number returned would use Count(), but wouldn't result in another query if the original query was materialized.

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();

这篇关于获取分页时的记录总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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