How to cache an IQueryable object?

I have this method that returns a Linq-to-SQL query for all rows in a UserStatus table:

public IQueryable<BLL.Entity.UserStatus> GetAll() { var query = from e in _SelectDataContext.UserStatus select new BLL.Entity.UserStatus { UserStatusId = e.UserStatusId, Enum = e.Enum, Name = e.Name }; return query; } 

This is just a lookup table that is unlikely to ever change, so I would like to cache the results. I can convert it to List<> and cache, but I would prefer to return an IQueryable object, since the other methods in the class depend on this. Can anyone help? Thanks.

+4
source share
4 answers

can query.ToList().AsQueryable() be the solution for you? not the best.

+7
source

You can't do this - Querable is a pointer rather than data. If you cache, you cache data, so you need to load data. Yapiskan has the right idea - cache the resulting ToList () and return it to AsQueryable ().

Please note that there will be no DataContext in the cache list, so your query parameters will be limited to the data loaded into the list.

+3
source

I needed to ask what do you really want to cache? Request or results?

If the results obtained above make sense, but if you want to cache the query for later use, I can suggest using CompiledQuery and then save it somewhere ...

Here's an article about CompiledQuery and how to use it.

Again, it all depends on whether you want to fulfill the request or not ...

+2
source

Take a look at the <Laboratory Library. This is a very good general purpose caching architecture.

0
source

Source: https://habr.com/ru/post/1286368/


All Articles