I have a fairly simple Linq query (simplified code):
dim x = From Product In lstProductList.AsParallel
Order By Product.Price.GrossPrice Descending Select Product
Product is a class. Product.Price is a child class and GrossPrice is one of its properties. In order to work out the price I need to use Session(“exchange_rate”).
So for each item in lstProductList there’s a function that does the following:
NetPrice=NetPrice * Session("exchange_rate")
(and then GrossPrice returns NetPrice+VatAmount)
No matter what I’ve tried I cannot access session state.
I have tried HttpContext.Current – but that returns Nothing.
I’ve tried Implements IRequiresSessionState on the class (which helps in a similar situation in generic http handlers [.ashx]) – no luck.
I’m using simple InProc session state mode. Exchange rate has to be user specific.
What can I do?
I’m working with:
web development, .Net 4, VB.net
Step-by-step:
page_load (in .aspx)
dim objSearch as new SearchClass()
dim output = objSearch.renderProductsFound()
then in objSearch.renderProductsFound:
lstProductList.Add(objProduct(1))
…
lstProductList.Add(objProduct(n))
dim x = From Product In lstProductList.AsParallel
Order By Product.Price.GrossPrice Descending Select Product
In Product.Price.GrossPrice Get :
return me.NetPrice+me.VatAmount
In Product.Price.NetPrice Get:
return NetBasePrice*Session(“exchange_rate”)
Again, simplified code, too much to paste in here. Works fine if I unwrap the query into For loops.
I’m not exactly sure how
HttpContext.Currentworks, but I wouldn’t be surprised if it would work only on the main thread that is processing the HTTP request. This would mean that you cannot use it on any other threads. When PLINQ executes the query, it picks some random threads from the thread pool and evaluates the predicates in the query using these threads, so this may be the reason why your query doesn’t work.If the
GrossPriceproperty needs to access only a single thing from the session state, it should be fairly easy to change it to a method and pass the value from the session state as an argument:Depending on where you use
xlater, you could also add a call toToListto force the evaluation of the query (otherwise it may be executed lazily at some later time), but I think the change I described above should fix it.