David Dickson
David Dickson

Reputation: 169

F# Power Pack Linq Issue

I have a simple function that makes use of the F# power pack to convert a quotation into a linq expression. The function is:

let toLinq (exp : Expr<'a -> 'b>) =
    let linq = exp.ToLinqExpression()
    let call = linq :?> MethodCallExpression
    let lambda = call.Arguments.[0] :?> LambdaExpression
    Expression.Lambda<Func<'a, 'b>>(lambda.Body, lambda.Parameters)

I use this function to create expressions that are consumed by a C# library that uses linq to sql to query a database. For example I might build an expression like:

    let test = toLinq (<@fun u -> u.FirstName = "Bob"@> : Expr<Account->bool>)

and pass it to a method like:

public IEnumerable<T> Find(Expression<Func<T, bool> predicate)
{
        var result = Table.OfType<T>();         
        result = result.Where(predicate)                                    
        var resultArray = result.ToArray();            
        return resultArray;
}

This was working as designed in verion 1.9.9.9 of the power pack. However it no longer works in the latest version of the power pack. The error I recieve is Method 'Boolean GenericEqualityIntrinsic[String](System.String, System.String)' has no supported translation to SQL.

I took a look at the changes to the power pack and it seems that the linq expression that is built using the new version makes use of GenericEqualityIntrinsic for comparing the property's value with the constant, whereas in version 1.9.9.9 it made use of String.op_Equality for comparison.

Is this a correct understanding of the issue? How do I make use of the new version of the power pack to convert quotations to linq expressions that can be consumed by a c# library that uses linq to sql?

Upvotes: 2

Views: 365

Answers (2)

Ankur
Ankur

Reputation: 33647

You can try the quotation as:

<@fun u -> u.FirstName.Equals("Bob")@>

Upvotes: 2

Brian
Brian

Reputation: 118865

Does explicitly calling

System.String.op_Equality(s1,s2)

work?

Upvotes: 2

Related Questions