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

编程入门 行业动态 更新时间:2024-10-23 05:50:18
本文介绍了如何使用 F# lambda 创建 Linq 表达式树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是在 C# 中可以做的事情 -

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

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

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

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

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>> ^^^^^^^^^^^^^^^^

推荐答案

我不知道您为什么要避免使用 F# 引用 - 在掩护下,它们与 C# 表达式树几乎相同,如果您愿意要在 F# 中创建表达式树,编译器将在任何情况下使用引号...

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

但是,这确实编译为包含在调用中的 F# 引用,该调用将其转换为 C# 表达式树.所以,最后,你会得到和你写的一样的东西:

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 表达式树?

本文发布于:2023-11-17 03:10:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608602.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   如何使用   lambda   Linq

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!