I am working on an ASP.NET application using LinqToSQL. When the page loads I run a query and save the results into a variable… var tasks = query expression. I then save this into a session variable Session["Tasks"] = tasks…
Is it possible to cast this session object back to its original var state, so I can run methods such as Count(), Reverse() and so on?
Thanks
varis just short-hand for type inference… the real question here is: what is the underlying type?If it involves an anonymous type (i.e. new {…}, or a
List<>there-of), then there is no elegant way (although it can be done in a hacky way). In short; don’t use anonymous types in this scenario…Note that a query expression such as
IQueryable<T>is not data – it is a query – to store the data (for a cache) you’d need to use.ToList()/.ToArray()etc.Important: you shouldn’t store a query expression in session; at best (in-memory session provider) that will keep the data-context alive; at worst (database etc session provider) it won’t work, as a data-context isn’t serializable. Storing results from a query is fine; but otherwise, rebuild the query expression per-request.
In which case, you might be able to use (for example):