Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a Linq expression tree with an F# lambda?

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>>
            ^^^^^^^^^^^^^^^^
like image 486
Bryan Edds Avatar asked Apr 18 '14 02:04

Bryan Edds


People also ask

What is expression trees and how they used in LINQ?

You can compile and run code represented by expression trees. This enables dynamic modification of executable code, the execution of LINQ queries in various databases, and the creation of dynamic queries. For more information about expression trees in LINQ, see How to use expression trees to build dynamic queries (C#).

What is an expression tree give an example?

The binary expression tree is a binary tree whose leaves are operands, such as constants or variable names, and the other nodes contain operators. For example, the postfix notation a b + c d e + * * results in the following expression tree.


1 Answers

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
like image 135
Tomas Petricek Avatar answered Oct 15 '22 21:10

Tomas Petricek