Linq:跨方法调用维护动态变量. [英] Linq: maintain dynamic variable across method calls.

查看:59
本文介绍了Linq:跨方法调用维护动态变量.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我正在使用linq,因此我动态创建了一些"var"类型的对象,如

Okay, so I am using linq and thus I have a few "var" types object created dynamically, as in

var Order = CustomerDataContext.Order.Where(e => e.OrderPending)

和许多其他类似的东西,所以问题在于如何维护此"var 在方法调用之间,我使用的是

and many others like this, so the problem is how can I maintain this "var" across method calls, I am using

ThreadPool.QueueUserForWorkItem(ProcessOrder, var)

,但是此方法接受对象,但我无法将参数中的back转换回Order类型.另外,由于它是动态的,因此甚至不能将其声明为静态的,这样我就可以保持状态,那我该怎么办?我应该如何维护此var,或者如果我不能维护该变量,那么做我正在做的事情的另一种方法是什么?

关键是,在我过滤并将此过滤后的记录传递给其他方法之后,如何获得其相同的记录集或说出相同的过滤后的列表,也要是该方法需要一个object类型的参数,因为您无法将其转换为Order类型,因为该var只是调用程序函数的局部变量.

我们仍然可以创建一个新的Order,但这会破坏过滤某些记录然后将这些记录传递给另一个要处理的方法的整个目的吗?如果在这种情况下,我们使用其他方法(例如

but this method accepts object and I cannot cast the back in parameter back into Order type. Also, since its dynamic it cannot even be declared as static, so that I may maintain state, so what should I do? How should I maintain this var, or if I cannot, then what is the other way to do what I am doing?

The point is, after I filter, and pass this filtered record to some other method, how do I get its same recordset or say same filtered list back, also if the method expects an argument of type object, as you cannot convert it into Order type because that var was just local variable to the caller function.

We can still create a new Order, but that would destroy the whole purpose of filtering some records and then passing those records to another method to be processed? If we create a new Order in other method say,

ProcessOrder

)创建新的Order,它将是一种新的Order类型,而不是在某些条件下过滤并作为参数传递给该方法的一种.希望我说清楚.

in this case, it will be a new Order type and not the one filtered on some condition and passed as parameter to this method. I hope I made my point clear.

Can any one help me with this?

推荐答案

这是我认为的解决方案,但不能确定.
linq语句的结果派生自IEnumerable T类型.您还可以执行以下操作:

Here is what I think is the solution, but cannot be sure.
The result of a linq statement is derived from type IEnumerable<T>. You can also do the following:

List<t> Order = CustomerDataContext.Order.Where(e => e.OrderPending).ToList()



这将返回List 的类型,为您提供List的所有灵活性,并且还意味着立即创建结果(Linq语句在使用之前不会被处理)



This will return a type of List<t>, gives you all the flexibilty of a List, and also means that the result created immediately (Linq statement is not processed until used)


这篇关于Linq:跨方法调用维护动态变量.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆