Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass LinQ Expressions from F# to C# code

Tags:

c#

linq

f#

ReactiveUI has methods with signitures like

public static ReactiveUI.ObservableAsPropertyHelper<TRet> 
  ObservableToProperty<TObj, TRet>(
    this TObj This, 
    System.IObservable<TRet> observable, 
    System.Linq.Expressions.Expression<System.Func<TObj, TRet>> property, 
    TRet initialValue = null, 
    System.Reactive.Concurrency.IScheduler scheduler = null
  )

In F# How would I construct an object like

System.Linq.Expressions.Expression<System.Func<TObj, TRet>> property, 

In C# I would do something like

this.ObservableAsPropertyHelper(
    observable,
    me => me.MyProperty
)

EDIT

I've tried

m.ObservableToProperty(this, value, fun me -> me.Property )

and

m.ObservableToProperty(this, 
            value, 
            new Linq.Expression.Expression(fun me -> me.Property )

but neither work

like image 311
bradgonesurfing Avatar asked Dec 14 '12 06:12

bradgonesurfing


1 Answers

I'm not sure if the new F# 3 query expressions help you, but the old PowerPack (which still works great!) has an extension method Expr.ToLinqExpression() : Expression, to compile F# quotation expressions (i.e. <@ fun me -> me.MyProperty @>) into LINQ expressions.

Edit: As Daniel had pointed out, QuotationToLambdaExpression does the works, for F# 3.

like image 175
Ramon Snir Avatar answered Nov 20 '22 12:11

Ramon Snir