在 pLinq 查询中使用会话状态
我有一个相当简单的 Linq 查询(简化的代码):
dim x = From Product In lstProductList.AsParallel
Order By Product.Price.GrossPrice Descending Select Product
Product 是一个类。 Product.Price 是一个子类,GrossPrice 是其属性之一。为了计算出价格,我需要使用 Session("exchange_rate")。
因此,对于 lstProductList 中的每个项目,都有一个函数执行以下操作:(
NetPrice=NetPrice * Session("exchange_rate")
然后 GrossPrice 返回 NetPrice+VatAmount)
无论我尝试什么,我都无法访问会话状态。
我尝试过 HttpContext.Current - 但没有返回任何内容。 我已经尝试在类上实现 IRequiresSessionState (这有助于通用 http 处理程序 [.ashx] 中的类似情况) - 没有运气。
我正在使用简单的 InProc 会话状态模式。汇率必须是用户特定的。
我能做些什么?
我正在与: Web 开发、.Net 4、VB.net
分步说明:
page_load(在 .aspx 中)
暗淡 objSearch 作为新的 SearchClass()
暗淡输出 = objSearch.renderProductsFound()
然后在 objSearch.renderProductsFound:
lstProductList.Add(objProduct(1))
...
lstProductList.Add(objProduct(n))
暗淡 x = 来自 lstProductList.AsParallel 中的产品
按 Product.Price.GrossPrice 降序排序 选择产品
在 Product.Price.GrossPrice 中获取:
返回me.NetPrice+me.VatAmount
在Product.Price.NetPrice中获取:
return NetBasePrice*Session("exchange_rate")
再次强调,简化的代码,太多,无法粘贴到此处。如果我将查询展开到 For 循环中,则效果很好。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不太确定 HttpContext.Current 是如何工作的,但如果它只在处理 HTTP 请求的主线程上工作,我不会感到惊讶。这意味着您不能在任何其他线程上使用它。当 PLINQ 执行查询时,它会从线程池中选取一些随机线程,并使用这些线程评估查询中的谓词,因此这可能是查询不起作用的原因。
如果 GrossPrice 属性只需要访问会话状态中的单个内容,那么将其更改为方法并将会话状态中的值作为参数传递应该相当容易:
取决于您在哪里稍后使用
x
,您还可以添加对ToList
的调用来强制对查询进行评估(否则它可能会在稍后的某个时间延迟执行),但我认为我上面描述的更改应该可以修复它。I'm not exactly sure how
HttpContext.Current
works, 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
GrossPrice
property 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
x
later, you could also add a call toToList
to 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.您确实应该将值从会话状态读取到您知道在 LINQ 语句中需要的局部变量中。否则,当值本质上是常量时,您实际上每次都会访问每个线程中每个元素的 NameValueCollection 实例。
You should really read the values out of session state and into local variables that you know you need within the LINQ statement. Otherwise you're actually accessing the NameValueCollection instance every single time for every single element in every single thread when the value is essentially constant.