LINQ:分页技术,使用take和skip但也需要总记录 - 如何实现?

发布于 2024-10-04 00:21:28 字数 267 浏览 7 评论 0原文

我已经使用skip 和take 实现了分页例程。它工作得很好,但在调用 Take 和 Skip 之前我需要表中的记录总数。

我知道我可以提交 2 个单独的查询。

  1. Get Count
  2. Skip and Take

但我不想向 LINQ 发出 2 次调用。

如何在同一查询中返回它(例如使用嵌套选择语句)?

以前,我在存储过程中使用了分页技术。我使用临时表返回了这些项目,并将计数传递给了输出参数。

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.

I know I can submit 2 separate queries.

  1. Get Count
  2. Skip and Take

But I would prefer not to issue 2 calls to LINQ.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花海 2024-10-11 00:21:28

抱歉,但你不能。至少,不是以一种漂亮的方式。

你可以用一种不漂亮的方式来做,但我认为你不喜欢那样:

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
    };

你明白吗?一点也不漂亮。

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.

独守阴晴ぅ圆缺 2024-10-11 00:21:28

没有理由执行两个单独的查询甚至存储过程。完成后,使用 let 绑定来记录子查询,您可以拥有一个包含所选项目以及总计数的匿名类型。对数据库进行一次查询,1 个 linq 表达式即可完成。要获取值,可以使用 jobQuery.Select(x => x.item) 或 jobQuery.FirstOrDefault().Count

Let 表达式是一件令人惊奇的事情。

var jobQuery = (
                from job in jc.Jobs
                let jobCount = (
                                    from j in jc.Jobs
                                    where j.CustomerNumber.Equals(CustomerNumber)
                                    select
                                        j
                                ).Count()
                where job.CustomerNumber.Equals(CustomerNumber)
                select
                new
                {
                    item = job.OrderBy(x => x.FieldName).Skip(0).Take(100),
                    Count = jobCount
                }
            );

There is no reason to do two seperate queries or even a stored procedure. Use a let binding to note a sub-query when you are done you can have an anon type that contains both your selected item as well as your total count. A single query to the database, 1 linq expression and your done. TO Get the values it would be jobQuery.Select(x => x.item) or jobQuery.FirstOrDefault().Count

Let expressions are an amazing thing.

var jobQuery = (
                from job in jc.Jobs
                let jobCount = (
                                    from j in jc.Jobs
                                    where j.CustomerNumber.Equals(CustomerNumber)
                                    select
                                        j
                                ).Count()
                where job.CustomerNumber.Equals(CustomerNumber)
                select
                new
                {
                    item = job.OrderBy(x => x.FieldName).Skip(0).Take(100),
                    Count = jobCount
                }
            );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文