views:

24

answers:

2

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?

+1  A: 

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.

James Curran
This is a good solution but unfortunately doesn't work for me because the data set would be too large to cache in memory.
Noel
+1  A: 

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();
David B
I like this idea! It will invalidate when a customer on that page is deleted or edited. However if you add a new customer and it changes the results in this list, I don't think a dependency based on the second query will be invalidated.
Noel