如何将Linq表达式从F#传递给C#代码

bra*_*ing 6 c# linq f#

ReactiveUI的方法有像

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
  )
Run Code Online (Sandbox Code Playgroud)

在F#中我如何构造一个像这样的对象

System.Linq.Expressions.Expression<System.Func<TObj, TRet>> property, 
Run Code Online (Sandbox Code Playgroud)

在C#中,我会做类似的事情

this.ObservableAsPropertyHelper(
    observable,
    me => me.MyProperty
)
Run Code Online (Sandbox Code Playgroud)

编辑

我试过了

m.ObservableToProperty(this, value, fun me -> me.Property )
Run Code Online (Sandbox Code Playgroud)

m.ObservableToProperty(this, 
            value, 
            new Linq.Expression.Expression(fun me -> me.Property )
Run Code Online (Sandbox Code Playgroud)

但都没有工作

Ram*_*nir 5

我不确定新的 F# 3 查询表达式是否对您有帮助,但旧的 PowerPack(仍然很好用!)有一个扩展方法Expr.ToLinqExpression() : Expression,可以将 F# 引号表达式(即<@ fun me -> me.MyProperty @>)编译为 LINQ 表达式。

编辑: 正如 Daniel 所指出的,QuotationToLambdaExpression可以为 F# 3 完成工作。

  • 看起来 [`QuotationToLambdaExpression`](http://msdn.microsoft.com/en-us/library/hh324072.aspx) 是在 F# 3.0 中执行此操作的方法。 (8认同)