转换Linq表达式

时间:2011-01-10 11:35:24

标签: c# linq linq-expressions

我不确定我正在努力实现的是什么,因此 - 写了一些代码:

public class Foo{
 public Bar Bar{get;set;}
}

public class Bar{
 public string Fizz{get;set;}
}

public class Facts{
 [Fact]
 public void fact(){
   Assert.Equal(expectedExp(),barToFoo(barExp()));
 }
 private Expression<Func<Foo,bool>> expectedExp(){
   return f=>f.Bar.Fizz=="fizz";
 }
 private Expression<Func<Bar,bool>> barExp(){
   return b=>b.Fizz=="fizz";
 }
 private Expression<Func<Foo,bool>> barToFoo
  (Expression<Func<Bar,bool>> barExp){
   return Voodoo(barExp); //<-------------------------------------------???
 }          
}

这甚至可能吗?

1 个答案:

答案 0 :(得分:2)

public class Foo
{
    public Bar Bar { get; set; }
}

public class Bar
{
    public string Fizz { get; set; }
}

private Expression<Func<Bar, bool>> BarExp()
{
    return b => b.Fizz == "fizz";
}

private Expression<Func<Foo, bool>> BarToFoo(Expression<Func<Bar, bool>> barExp)
{
    Expression<Func<Foo, Bar>> barMemberAccessExpression = foo => foo.Bar;

    var fooParam = Expression.Parameter(typeof(Foo), "foo");

    var invokeExpression = Expression.Invoke(
        barExp,
        Expression.Invoke(barMemberAccessExpression, fooParam)
        );

    var result = Expression.Lambda<Func<Foo, bool>>(invokeExpression, fooParam);

    return result;

    //Expression<Func<Foo, bool>> result =
    //    foo => barExp.Compile().Invoke(foo.Bar);

    //return result;
}

[Test]
public void TestBarToFoo()
{
    var fooInstance = new Foo { Bar = new Bar() { Fizz = "fizz" } };

    var barExpr = this.BarExp();

    Console.WriteLine(
        barExpr.Compile().Invoke(fooInstance.Bar)); // = True

    Console.WriteLine(
        this.BarToFoo(barExpr).Compile().Invoke(fooInstance)); // = True
}