转换F#报价为LINQ表达式

编程入门 行业动态 更新时间:2024-10-22 12:33:22
本文介绍了转换F#报价为LINQ表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以转换类型 Expr的℃的报价;'A - > 'B> 来通过下面的代码片段一个LINQ表达式:

I can convert a quotation of type Expr<'a -> 'b> to a Linq expression via the following snippet:

/// Converts a F# Expression to a LINQ Lambda let toLambda (exp:Expr) = let linq = exp.ToLinqExpression() :?> MethodCallExpression linq.Arguments.[0] :?> LambdaExpression /// Converts a Lambda quotation into a Linq Lamba Expression with 1 parameter let ToLinq (exp : Expr<'a -> 'b>) = let lambda = toLambda exp Expression.Lambda<Func<'a, 'b>>(lambda.Body, lambda.Parameters)

现在我想转换型报价 Expr的<A *b - > C> 或者甚至 Expr的<'A - > B - > C> 来型的Linq Lambda表达式表达式来; Func键<'一'B'C>>

Now I want to convert a quotation of type Expr<'a * 'b -> 'c> or maybe even Expr<'a -> 'b -> 'c> to a Linq Lambda Expression of type Expression<Func<'a,'b'c>>.

我怎样才能做到这一点?

How can I do this?

问候, forki

Regards, forki

推荐答案

我不知道这是直接由F#PowerPack中提供的LINQ模块的支持。但是,您可以实现通过F#库产生的LINQ表达自己的后处理把它变成了一般形式的C#lambda函数:

I'm not sure if this is directly supported by the LINQ modules available in the F# PowerPack. However, you can implement your own post-processing of the LINQ expression produced by F# libraries to turn it into a C# lambda function of the usual form:

下面的函数取其被构造为单个参数的多个嵌套 LambdaExpression 表达式(即,被F#翻译产生的结构),并返回所述内的参数的列表和身体LINQ表达式 - 大多数表达式:

The following function takes a LINQ expression which is constructed as multiple nested LambdaExpression expressions of a single parameter (that is, the structure produced by F# translator) and returns a list of parameters and body of the inner-most expression:

let rec translateExpr (linq:Expression) = match linq with | :? MethodCallExpression as mc -> let le = mc.Arguments.[0] :?> LambdaExpression let args, body = translateExpr le.Body le.Parameters.[0] :: args, body | _ -> [], linq

现在你可以用它来获得一个普通的 Func键委托出去的类型,例如 INT - > INT - > INT - > INT 是这样的:

Now you can use it to get an ordinary Func delegate out of type such as int -> int -> int -> int like this:

let linq = (<@@ fun a b c -> (a + b) * c @@>).ToLinqExpression() let args, body = translateExpr liq let f = Expression.Lambda<Func<int, int, int, int>> (body, args |> Array.ofSeq) f.Compile().Invoke(10, 11, 2)

更多推荐

转换F#报价为LINQ表达式

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

发布评论

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

>www.elefans.com

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