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

泄露秘密 提交于 2019-12-22 05:30:11

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/13874032/how-to-pass-linq-expressions-from-f-to-c-sharp-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!