LINQ:分页技术,使用“接"和“跳过",但也需要总记录-如何实现此目的? [英] LINQ: Paging technique, using take and skip but need total records also - how to implement this?
问题描述
我已经使用跳过和获取实现了分页例程.效果很好,但是在调用Take和Skip之前,我需要表中的记录总数.
I have implemented a paging routine using skip and take. It works great, but I need the total number of records in the table prior to calling Take and Skip.
我知道我可以提交2个单独的查询.
I know I can submit 2 separate queries.
- 计数
- 跳过并获取
但是我不希望对LINQ发出2次呼叫.
But I would prefer not to issue 2 calls to LINQ.
如何在同一查询中返回它(例如,使用嵌套的select语句)?
How can I return it in the same query (e.g. using a nested select statement)?
以前,我在存储过程中使用了分页技术.我使用临时表返回了项目,并将计数传递给输出参数.
Previously, I used a paging technique in a stored procedure. I returned the items by using a temporary table, and I passed the count to an output parameter.
推荐答案
对不起,您不能.至少不是很漂亮.
I'm sorry, but you can't. At least, not in a pretty way.
您可以用一种不太漂亮的方式来做到这一点,但我认为您不喜欢这样:
You can do it in an unpretty way, but I don't think you like that:
var query = from e in db.Entities where etc etc etc;
var pagedQuery =
from e in query.Skip(pageSize * pageNumber).Take(pageSize)
select new
{
Count = query.Count(),
Entity = e
};
你看到了吗?一点都不漂亮.
You see? Not pretty at all.
这篇关于LINQ:分页技术,使用“接"和“跳过",但也需要总记录-如何实现此目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!