用Expression< Func< T,bool>>组成调用.与Func< bool>相同. [英] Composing invocations with Expression<Func<T,bool>> the same way as Func<T,bool>

查看:93
本文介绍了用Expression< Func< T,bool>>组成调用.与Func< bool>相同.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个可用作多个其他类的成员的类:

Consider a class that can be used as a member of multiple other classes:

class Customer {
    public string FirstName {get;set;}
    public string LastName {get;set;}
}
// Both "Order" and "Profile" have a "Customer" property
class Order {
    public Customer Customer {get;set;}
}
class Profile {
    public Customer Customer {get;set;}
}

我想定义一个方法来检查与Customer关联的对象.如果我想要一个内存中检查器,可以这样:

I want to define a method that makes a checker for an object associated with a Customer. If I want an in-memory checker, I do it like this:

static Func<T,bool> Check<T>(Func<T,Customer> conv, string first, string last) {
    return obj => conv(obj).FirstName == first && conv(obj).LastName == last;
}

我可以将检查器用于内存中序列,如下所示:

I can use my checker for in-memory sequences as follows:

var matchingOrders = orders
    .Where(Check<Order>(x => x.Customer, "Foo", "Bar"))
    .ToList();
var matchingProfiles = profiles
    .Where(Check<Profile>(x => x.Customer, "Foo", "Bar"))
    .ToList();

现在我想对Expression<Func<T,bool>>做同样的事情:

Now I want to do the same thing with Expression<Func<T,bool>>:

static Expression<Func<T,bool>> Check<T>(Expression<Func<T,Customer>> conv, string first, string last)

不幸的是,相同的技巧不起作用:

Unfortunately, the same trick does not work:

return obj => conv(obj).FirstName == first && conv(obj).LastName == last;

并像这样使用它:

var matchingOrders = dbContext.Orders
    .Where(Check<Order>(x => x.Customer, "Foo", "Bar"))
    .ToList();
var matchingProfiles = dbContext.Profiles
    .Where(Check<Profile>(x => x.Customer, "Foo", "Bar"))
    .ToList();

这会触发错误:

CS0119:表达式表示预期存在variable', where a方法组

我可以像编写委托一样来编写表达式吗?

Can I compose expressions the same way that I compose delegates?

推荐答案

不幸的是,C#当前不提供从Expression<Func<...>>对象组成表达式的方法.您必须使用更长的表达式树:

Unfortunately, C# does not currently provide a way to compose expressions from Expression<Func<...>> objects. You have to use expression trees, which is quite a bit longer:

static Expression<Func<T,bool>> CheckExpr<T>(Expression<Func<T,Customer>> conv, string first, string last) {
    var arg = Expression.Parameter(typeof(T));
    var get = Expression.Invoke(conv, arg);
    return Expression.Lambda<Func<T,bool>>(
        Expression.MakeBinary(
            ExpressionType.AndAlso
        ,   Expression.MakeBinary(
                ExpressionType.Equal
            ,   Expression.Property(get, nameof(Customer.FirstName))
            ,   Expression.Constant(first)
            )
        ,   Expression.MakeBinary(
                ExpressionType.Equal
            ,   Expression.Property(get, nameof(Customer.LastName))
            ,   Expression.Constant(last)
            )
        )
    ,   arg
    );
}

这篇关于用Expression&lt; Func&lt; T,bool&gt;&gt;组成调用.与Func&lt; bool&gt;相同.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆