views:

17

answers:

1

I found this question http://stackoverflow.com/questions/343899/how-to-cache-data-in-a-mvc-application and I'm wondering about being able to use this method with IQueryable data.

    Public Function GetUsers() As IQueryable(Of User) Implements IUserRepository.GetUsers 
        Dim users = (From u In dc.Users 
                    Select u) 
        Return users.AsQueryable 
    End Function 

Can I change this to

    Public Function GetUsers() As IQueryable(Of User) Implements IUserRepository.GetUsers 
       Dim users = Cache("users")
       If users Is Nothing Then
           users = (From u In dc.Users 
                           Select u) 
           Cache("users") = users
       End If

       Return users.AsQueryable
    End Function
+1  A: 

You can do it yes, but it won't have the desired effect since (assuming dc is a linq to sql data context) an IQueryable is not a collection of data, it is a stored query which will execute later, you will need to store an IList or similar, you could still return it as an IQueryable if desired, though that could be confusing to others.

Use ToList() before caching it.

Paul Creasey
this is exactly what I thought. I'm thinking my better option is to use the caching in my service layer instead of my repo layer (counter to what someone else told me). I use AsQueryable in my repo layer, and then I call my repo from my service layer. I'll cache in there.Thanks for the insights.
rockinthesixstring