当使用“from ... select”表单时,我可以使用let
语句在Linq中分配局部变量。如何用lambdas捕获变量?我需要的非工作示例:
var result = list.Select(a =>
let localVariable = a.number + 2 // <- obviously non working
new {
Variable = localVariable
}
);
答案 0 :(得分:13)
这应该有效:
var result = list.Select(a =>
{
var localVariable = a.number + 2;
return new
{
Variable = localVariable
};
}
);