Home Acessing and using a cached var for LINQ
Reply: 0

Acessing and using a cached var for LINQ

user1179 Published in June 25, 2018, 8:01 am

I am trying to use a cached variable for LINQ and I'm having issues accessing the cached variable in order to run further select LINQ statements.


string cachedsearchname = "MyCachedVar";
var output = HttpContext.Cache[cachedsearchname];

if (output == null)
    output = (from l in db.vwMyView select l)
    HttpContext.Cache.Insert(cachedsearchname, output);

var output2 = (List<vwMyView>)HttpContext.Cache[cachedsearchname];

The last line throws the error:

System.InvalidCastException: 'Unable to cast object of type 'System.Data.Entitiy.Infrastracture.DbQuery'1[vwMyView]' to type 'System.Collections.Generic.List'1[vwMyView]'

My goal would be to run:

if (PassedQuerystring.Contains("MyRequestedOption")
    output = (from m in output2 where m.myrequestedoption
                                       select m)

Challenge is I don't want to convert ToList() yet as the cached result is quite large.

Any input much appreciated.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308593 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO