【发布时间】:2012-04-09 00:07:13
【问题描述】:
我正在阅读C# 4 in a Nutshell,我来到了这段代码:
IQueryable<Product> SearchProducts (params string[] keywords)
{
IQueryable<Product> query = dataContext.Products;
foreach (string keyword in keywords)
{
string temp = keyword;
query = query.Where (p => p.Description.Contains (temp));
}
return query;
}
在代码之后有一个“警告”,如下所示:
The temporary variable in the loop is required to avoid the outer variable trap, where the same variable is captured for each iteration of the foreach loop.
我不明白,我不明白为什么需要 temp 变量。 outter variable trap 是什么?
发件人:http://www.albahari.com/nutshell/predicatebuilder.aspx
有人可以澄清一下吗?
【问题讨论】:
-
你肯定想看this article。
-
每个人都推荐了同一篇博文,为什么我以前从未听说过 Eric?谢谢大家
-
@sebastian:我们不知道为什么你以前没有听说过他,但你还有很多事情要做。 Eric 的博客是一个宝库。享受愉快的几个月阅读吧:)
-
相关:stackoverflow.com/questions/7133816(罪魁祸首是
foreach)
标签: linq c#-4.0 lambda iqueryable