如何创建一个F#的lambda一个LINQ表达式树? [英] How do I create a Linq expression tree with an F# lambda?
问题描述
下面就是可以用C#来完成 -
VAR 2 = 2;
System.Linq.Expressions.Expression< System.Func< INT,INT>> EXPR = X => X * 2;
expr.Compile()调用(4)。 //返回8
我希望做在F#的精确等价的。这是我尝试过,但没有编译 -
让两人= 2
让EXPR =(好玩点¯x - > ; X *二):System.Linq.Expressions.Expression< System.Func< INT,INT>>
expr.Compile()调用(4)//想返回8
也许不出所料,编译于2号线失败,出现以下错误 -
这个函数的参数太多,或在上下文中使用在未预期的函数。
让EXPR =(好玩点¯x - > X *二):System.Linq.Expressions.Expression< System.Func< INT,INT>>
^^^^^^^^^^^^^^^^
我不知道为什么你要避免使用F#报价 - 掩护下,他们是几乎同样的事情,C#表达式树,如果你想建立在F#中的表达式树,编译器将使用报价的掩护下,在任何情况下...
无论如何,你可以做到这一点没有明确写入< @ .. @>
,因为当它作为参数的方法传递的编译器可以自动引用的函数。所以,你可以这样做:
<预类=郎毫升prettyprint-覆盖>
键入Expr的=
上配置静态成员引用(E:表达式来; System.Func< INT,INT>>)= E
让两人= 2
让EXPR = Expr.Quote(好玩点¯x - > X *二)
expr.Compile()调用(4)//想返回8
编辑:然而,这真的编译成一个F#报价包裹在它转换成C#表达式树的电话。所以,到最后,你会得到同样的事情,如果你写的:
<预类=郎毫升prettyprint-覆盖>
开放Microsoft.FSharp.Linq.RuntimeHelpers
让两人= 2
让EXPR =
< @ System.Func< _,_>(好玩点¯x - > X *两)@>
|> LeafExpressionConverter.QuotationToExpression
|>拆箱<表达式来; Func键< INT,INT>>>
expr.Compile()调用(4)//想返回8
Here's what can be done in C# -
var two = 2;
System.Linq.Expressions.Expression<System.Func<int, int>> expr = x => x * two;
expr.Compile().Invoke(4); // returns 8
I wish to do the precise equivalent in F#. Here's what I tried, but did not compile -
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
Perhaps predictably, compilation fails on line 2 with the following error -
"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>>
^^^^^^^^^^^^^^^^
I'm not sure why you want to avoid using F# quotations - under the cover, they are pretty much the same thing as C# expression trees and if you want to create an expression tree in F#, the compiler will be using quotations under the cover in any case...
Anyway, you can do this without writing explicit <@ .. @>
because the compiler can automatically quote a function when it is passed as an argument to a method. So you can do:
type Expr =
static member Quote(e:Expression<System.Func<int, int>>) = e
let two = 2
let expr = Expr.Quote(fun x -> x * two)
expr.Compile().Invoke(4) // desired to return 8
EDIT: However, this really compiles to an F# quotation wrapped in a call that converts it to C# expression tree. So, in the end, you'll get the same thing as if you wrote:
open Microsoft.FSharp.Linq.RuntimeHelpers
let two = 2
let expr =
<@ System.Func<_, _>(fun x -> x * two) @>
|> LeafExpressionConverter.QuotationToExpression
|> unbox<Expression<Func<int, int>>>
expr.Compile().Invoke(4) // desired to return 8
这篇关于如何创建一个F#的lambda一个LINQ表达式树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!