C#中:是否有可能在一个匿名方法来声明一个局部变量? [英] C#: Is it possible to declare a local variable in an anonymous method?
问题描述
时,可以在下面的代码在一个匿名的C#方法的局部变量,即我想只执行一次计数。
Is is possible to have a local variable in an anonymous c# methods, i.e. in the following code I would like to perform the count only once.
IQueryable<Enquiry> linq = db.Enquiries;
if(...) linq = linq.Where(...);
if(...) linq = linq.Where(e =>
(x <= (from p in db.Orders where p.EnquiryId == e.Id select p).Count() &&
(from p in db.Orders where p.EnquiryId == e.Id select p).Count() <= y));
if(...) linq = linq.Where(...);
var result = (from e in linq select e);
有没有让匿名函数?
Is there a "let" for anonymous functions?
更新:
请注意,我添加几个凡该语句之后子句,所以我不能用一个选择关闭
Update: Note that I'm adding several Where clauses after this statement so I can't close with a select.
/尼尔斯
推荐答案
我碰到类似的问题。该解决方案是创建一个自定义表达式树生成方法。
I've run into a similar problem. The solution is to create a custom expression tree generating method.
我问在MSDN上的论坛的问题。请参阅问题和答案在这里:的重用哪里表达式。
I asked my question on MSDN-forums. Please see the question and answer here: Reusing Where expressions.
这可能给你如何进行的想法,但我必须承认,自定义表达式树木不适合胆小的人; - )
This may give you an idea on how to proceed, but I must admit that custom expression trees are not for the faint-hearted ;-)
这篇关于C#中:是否有可能在一个匿名方法来声明一个局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!