Expression<Func<T, bool>> from a F# func
f#, linq
Solution
I can't remember off the top of my head where I found this bit of code, but this is what I use to convert an `Expr<'a -> 'b>` to `Expression<Func<'a, 'b>>`. Hopefully this will solve your problem.
open System
open System.Linq.Expressions
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Linq.QuotationEvaluation
let toLinq (expr : Expr<'a -> 'b>) =
let linq = expr.ToLinqExpression()
let call = linq :?> MethodCallExpression
let lambda = call.Arguments.[0] :?> LambdaExpression
Expression.Lambda<Func<'a, 'b>>(lambda.Body, lambda.Parameters)
Problem
in linq, .Where takes a Expression> predicate, which I can write in F# as ``` <@ fun item:'a -> condition @> // Expr<'a -> bool> ``` I'm using FSharp.Powerpack to build the expression from a quotation, but what it gives me is a MethodCallExpression. Looking deep, the powerpack code builds the lambda correctly, but wraps it in a Convert call (why is that?). I wonder if casting the argument to the method call (a lambda) would finally give me the Expression> I need. So the question is why the Convert call, and how to actually get the lambda with the Func signature.