使用外部表达式参数的成员属性调用 C# 内部表达式

发布于 2024-10-02 14:44:46 字数 1785 浏览 2 评论 0 原文

我正在使用在这里找到的 Albaharis PredicateBuilder http://www.albahari.com/nutshell/predicatebuilder。 aspx 来过滤 Linq-to-SQL 应用程序中的结果。这一直很有效。

我现在想做的是重用现有的过滤谓词表达式来过滤将现有过滤对象作为属性的对象。

例如,我有 2 个类,OrderCustomer。我已经有一个返回 Expression> 的方法,该方法是使用上述谓词构建器构建的。我现在想在我的 Order 过滤方法中重用它,该方法将通过某种方式传递 Order.Customer 返回 Expression> 属性(表达式?)到我的 Customer 过滤器方法中。

我有这样的东西(远未完成,但我希望你明白):

public class CustomerSearchCriteria
{
    public Expression<Func<Customer, bool>> FilterPredicate()
    {
        // Start with predicate to include everything
        var result = PredicateBuilder.True<Customer>();

        // Build predicate from criteria
        if (!String.IsNullOrEmpty(this.Name))
        {
            result = result.And(c => SqlMethods.Like(c.Name, this.Name));
        }

        // etc. etc. etc

} 


public class OrderSearchCriteria
{
    public Expression<Func<Order, bool>> FilterPredicate()
    {
        // Start with predicate to include everything
        var result = PredicateBuilder.True<Order>();

        // Build predicate from criteria
        if (!String.IsNullOrEmpty(this.Reference))
        {
            result = result.And(o => SqlMethods.Like(o.Reference, this.Reference));
        }

        // etc. etc. etc
        // This is where I would like to do something like:
        // result = result.And(o => o.Customer "matches" this.CustomerCriteria.FilterPredicate()
} 

任何 Linq 表达式大师都可以帮助我吗?

提前致谢。

I am using the Albaharis PredicateBuilder as found here http://www.albahari.com/nutshell/predicatebuilder.aspx to filter results in a Linq-to-SQL application. This has been working great.

What I am trying to do now, is reuse the existing filtering predicate expression to filter an object that has the existing filtered object as a property.

For example, I have 2 classes, Order and Customer. I already have a method that returns a Expression<Func<Customer, bool>>, which is built using the above mentioned predicate builder. I now want to reuse this in my Order filtering method, which will return a Expression<Func<Customer, bool>> by somehow passing the Order.Customer property (expression?) into my Customer filter method.

I have something like this (far from complete, but I hope you get the idea):

public class CustomerSearchCriteria
{
    public Expression<Func<Customer, bool>> FilterPredicate()
    {
        // Start with predicate to include everything
        var result = PredicateBuilder.True<Customer>();

        // Build predicate from criteria
        if (!String.IsNullOrEmpty(this.Name))
        {
            result = result.And(c => SqlMethods.Like(c.Name, this.Name));
        }

        // etc. etc. etc

} 


public class OrderSearchCriteria
{
    public Expression<Func<Order, bool>> FilterPredicate()
    {
        // Start with predicate to include everything
        var result = PredicateBuilder.True<Order>();

        // Build predicate from criteria
        if (!String.IsNullOrEmpty(this.Reference))
        {
            result = result.And(o => SqlMethods.Like(o.Reference, this.Reference));
        }

        // etc. etc. etc
        // This is where I would like to do something like:
        // result = result.And(o => o.Customer "matches" this.CustomerCriteria.FilterPredicate()
} 

Can any Linq expression guru help me?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚气少女 2024-10-09 14:44:46

如果您使用 Albaharis 的 LinqKit,您应该能够执行以下操作:

var customerFilter = this.CustomerCriteria.FilterPredicate();
// create an expression that shows us invoking the filter on o.Customer
Expression<Func<Order, bool>> customerOrderFilter = 
    o => customerFilter.Invoke(o.Customer);
// "Expand" the expression: this creates a new expression tree
// where the "Invoke" is replaced by the actual predicate.
result = result.And(customerOrderFilter.Expand())

If you use the Albaharis' LinqKit, you should be able to do something like this:

var customerFilter = this.CustomerCriteria.FilterPredicate();
// create an expression that shows us invoking the filter on o.Customer
Expression<Func<Order, bool>> customerOrderFilter = 
    o => customerFilter.Invoke(o.Customer);
// "Expand" the expression: this creates a new expression tree
// where the "Invoke" is replaced by the actual predicate.
result = result.And(customerOrderFilter.Expand())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文