相关疑难解决方法(0)

如何使用F#lambda创建Linq表达式树?

这是C#可以做的 -

var two = 2;
System.Linq.Expressions.Expression<System.Func<int, int>> expr = x => x * two;
expr.Compile().Invoke(4); // returns 8
Run Code Online (Sandbox Code Playgroud)

我希望在F#中做到精确的等价物.这是我尝试过的,但没有编译 -

let two = 2
let expr = (fun x -> x * two) : System.Linq.Expressions.Expression<System.Func<int, int>>
expr.Compile().Invoke(4) // desired to return 8
Run Code Online (Sandbox Code Playgroud)

也许可以预见,编译在第2行失败,出现以下错误 -

"This function takes too many arguments, or is used in a context where a function is not expected."
let expr = (fun x -> x * two) : System.Linq.Expressions.Expression<System.Func<int, int>>
            ^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

c# linq f# expression-trees

16
推荐指数
1
解决办法
2502
查看次数

标签 统计

c# ×1

expression-trees ×1

f# ×1

linq ×1