C#如何将Expression <Func <SomeType >>转换为Expression <Func <OtherType >>

Mic*_*out 15 c# lambda expression-trees repository-pattern linq-to-sql

我之前使用过基于lamdas的C#表达式,但我没有手工编写它们的经验.鉴于Expression<Func<SomeType, bool>> originalPredicate,我想创建一个Expression<Func<OtherType, bool>> translatedPredicate.

在这种情况下,SomeType和OtherType具有相同的字段,但它们不相关(没有继承而不是基于公共接口).

背景:我有一个基于LINQ to SQL的存储库实现.我将LINQ to SQL实体投影到我的Model实体,以便将我的模型保存在POCO中.我想将表达式传递给存储库(作为规范的一种形式),但它们应该基于模型实体.但我无法将这些表达式传递给数据上下文,因为它需要基于LINQ to SQL实体的表达式.

Mar*_*ell 21

有了Expression,最简单的方法是使用转换表达式:

class Foo {
    public int Value { get; set; }
}
class Bar {
    public int Value { get; set; }
}
static class Program {
    static void Main() {
        Expression<Func<Foo, bool>> predicate =
            x => x.Value % 2 == 0;
        Expression<Func<Bar, Foo>> convert =
            bar => new Foo { Value = bar.Value };

        var param = Expression.Parameter(typeof(Bar), "bar");
        var body = Expression.Invoke(predicate,
              Expression.Invoke(convert, param));
        var lambda = Expression.Lambda<Func<Bar, bool>>(body, param);

        // test with LINQ-to-Objects for simplicity
        var func = lambda.Compile();
        bool withOdd = func(new Bar { Value = 7 }),
             withEven = func(new Bar { Value = 12 });
    }
}
Run Code Online (Sandbox Code Playgroud)

但请注意,不同的提供商将以不同的方式支持此功能.例如,EF可能不喜欢它,即使LINQ-to-SQL也是如此.

另一种选择是重建表达式树完全,使用反射来找到对应的成员.更复杂.