【发布时间】:2018-12-29 08:11:40
【问题描述】:
我想在 select 令牌中使用 lambda-expression。下面是一个简化的例子:
// Example-0; It is NOT compilable.
var xs = from v in Enumerable.Range( 0, 4 ) select w => w;
但是,该示例无法编译。 (我使用的是 C#-7.0/.net Framework 4.7.2)
错误 CS1942:
select' clause is incorrect. Type inference failed in the call toSelect' 中的表达式类型
我在下面尝试了另一种类似的模式:
// Example-1; It can compile.
Func< int, int > f = w => w;
var xs = from v in Enumerable.Range( 0, 4 ) select ( v, f );
但是,Example-1 很混乱,它无法捕获select 标记中的值。
// Example-2; It is NOT compilable.
var xs = from v in Enumerable.Range( 0, 4 ) select w => w + v;
如何编码?
【问题讨论】:
标签: c# linq select lambda capture