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

Acessing and using a cached var for LINQ

AMorrisey
1#
AMorrisey Published in 2018-01-12 14:23:11Z

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.

Code:

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
                                       .Contains("MyRequestedOption")
                                       select m)
}

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

Any input much appreciated.

Nick Larsen
2#
Nick Larsen Reply to 2018-01-12 14:25:48Z

When you retrieve from the cache, you need to cast to the same datatype that went into the cache.

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO