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

Acessing and using a cached var for LINQ

user7974
1#
user7974 Published in September 19, 2018, 3:17 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.

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.

share|improve this question
  • 1
    Why not throw a ToList on the end of the query? That would cache the data and not the query. – juharr Jan 12 at 14:25
  • Why do you want to cache it, if not so that the "quite large" results are only obtained once? – Jon Hanna Jan 12 at 14:29
  • Adding ToList() adds a huge time expense when running and yes - I want to cache in order to do this once so next time its called, no need to hit the db. (Performance reasons) – AMorrisey Jan 12 at 14:42

1 Answer 1

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO