【问题标题】:Caching paged LINQ results without using Skip() or Take()在不使用 Skip() 或 Take() 的情况下缓存分页的 LINQ 结果
【发布时间】:2011-04-03 11:50:54
【问题描述】:

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

【问题讨论】:

  • 如何混合 Linq 和 SqlDependency?

标签: sql-server linq paging sqlcachedependency


【解决方案1】:

分两次提取数据:

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

【讨论】:

  • 我喜欢这个主意!当该页面上的客户被删除或编辑时,它将失效。但是,如果您添加一个新客户并更改此列表中的结果,我认为基于第二个查询的依赖项不会失效。
【解决方案2】:

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

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

【讨论】:

  • 这是一个很好的解决方案,但不幸的是对我不起作用,因为数据集太大而无法缓存在内存中。
猜你喜欢
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 2013-11-25
  • 1970-01-01
  • 2013-11-26
相关资源
最近更新 更多