Linq to SQL - 如何在没有两个查询的情况下分页时获取结果总数

发布于 2024-10-09 09:26:44 字数 235 浏览 0 评论 0原文

我的 IQueryable 服务有以下 LINQ 查询,

Dim eventcount = EventService.QueryEvents().Count
Dim eventlist = EventService.QueryEvents().Skip((page - 1) * 5).Take(5).ToList   

但我想知道是否有一种方法可以在不两次调用数据库的情况下执行此操作?

I've got the following LINQ query to my IQueryable service

Dim eventcount = EventService.QueryEvents().Count
Dim eventlist = EventService.QueryEvents().Skip((page - 1) * 5).Take(5).ToList   

But I'm wondering if there's a way to do this without making two calls to the database?

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

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

发布评论

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

评论(2

似梦非梦 2024-10-16 09:26:44

我会考虑在计数上实现 2 级缓存,小心地让缓存键包含相关标准,并适当地使其无效。

I would consider implementing level 2 caching on the count, being careful to get the caching key to contain the relevant criteria, and invalidating it appropriately.

陌上青苔 2024-10-16 09:26:44

好吧,我前段时间遇到了同样的问题,然后我想出了 LINQ to SP =)。Make 和 SP 并将其放入您的实体中并使用它。你可以根据你的需要写Sp,就像拉你的记录和总记录列一样。与您现在使用的相比,它更加简单和快速。

去年我遇到了同样的问题,然后当谈到性能时,我不再进行两个查询,而是转向 LINQ to SP,并创建一个存储过程并通过诸如 之类的实体调用它>LINQ 到存储过程 =)。我的存储过程完成了所有工作,只返回了我想要的那些记录,并且我在存储过程结束时进行了分页,以便我一次获得有限的数据,而不是应用程序中的所有数据。

您可以制作 SP 并将其放入您的实体中并使用它。您可以根据您的需要编写 Sp,例如将您的记录和总记录也拉为列。考虑有 100 个表的 1000K 条记录。性能慢??您可以通过 Linq to SQL 在数据库级别创建 Sp 并通过实体进行调用来实现这一点。它会比你想要实现的快得多 =)

Well i was facing the same issue some time back and then i came up with LINQ to SP =).Make and SP and drop that into your entities and use it. you can write Sp according to your need like pulling your records and total record column too. It is more easy and fast as compare to that whet you are using right now.

I was having the same issue last year and then when it came to performance, then instead of making two queries, I moved to LINQ to SP, and made a stored procedure and called it through entities like LINQ to stored procedure =). And my stored procedure did all and returned only those records that I wanted and I did paging to at the stored procedure end so that I got limited data at a time rather than all data at application.

You can make and SP and drop that into your entities and use it. you can write Sp according to your need like pulling your records and total records too as column . Consider having 1000K of records of 100 of tables. Slow performance ?? you can do that by Linq to SQL by making a Sp at database level and calling through entities. It will be much faster then the one you trying to achieve =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文