不使用 Skip() 或 Take() 缓存分页 LINQ 结果

发布于 2024-09-15 17:03:51 字数 272 浏览 2 评论 0原文

我在我的网站中使用 LINQ 来分页数据。我目前正在使用 Skip() 和 Take() 来执行分页。现在我想使用缓存依赖项来缓存数据,这样如果数据发生变化,缓存就会失效。但是,SQL Server 的查询通知不支持 TOP 表达式。是否有其他方法可以使用不生成 TOP 的 LINQ 查询分页数据集?或者另一种方法来缓存这些数据以使其失效?

I am using LINQ for paging data in my website. I am currently using Skip() and Take() to perform the paging. Now I want to cache the data using cache dependencies so that the the cache will invalidate if the data changes. However SQL Server's query notifications don't support the TOP expression. Are there any alternative ways to query a paged set of data with LINQ that doesn't generate TOP? Or another way to cache this data so that it invalidates?

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

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

发布评论

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

评论(2

夏雨凉 2024-09-22 17:03:51

分两遍提取数据:

// step1: get the IDs of the items in the current page.
List<int> customerIds = db.Customers
  .Where(filter)
  .OrderBy(c => c.FirstName)
  .ThenBy(c => c.CustomerID)
  .Select(c => c.CustomerID)
  .Skip(200)
  .Take(20)
  .ToList();

// step2: get the items for that page
List<Customer> customers = db.Customers
  .Where(c => customerIds.Contains(c.CustomerID))
  .ToList();

Pull your data in two passes:

// step1: get the IDs of the items in the current page.
List<int> customerIds = db.Customers
  .Where(filter)
  .OrderBy(c => c.FirstName)
  .ThenBy(c => c.CustomerID)
  .Select(c => c.CustomerID)
  .Skip(200)
  .Take(20)
  .ToList();

// step2: get the items for that page
List<Customer> customers = db.Customers
  .Where(c => customerIds.Contains(c.CustomerID))
  .ToList();
审判长 2024-09-22 17:03:51

缓存整个结果集,并将 SqlDependancy 设置为该结果集。

从缓存中读取整个集合,然后对其使用“Skip/Take”。

Cache the entire result set, and set the SqlDependancy to that.

Read the entire set from the cache, and then use Skip/Take on that.

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