如何从表达式树动态构造谓词方法?

发布于 2024-09-24 02:19:46 字数 1593 浏览 5 评论 0 原文

这是场景: Silverlight 4.0、DataGrid、PagedCollectionView 项目源。 目标是将过滤器应用于 PCV。过滤器需要是一个 Predicate(Method) - 其中 Method 针对对象实现一些逻辑并返回 true/false 来包含。 我需要在过滤器逻辑中选择性地包含 3 个不同的标准,并且显式代码很快就会变得丑陋。我们不希望那样,是吗?

所以我看到有一种方法可以使用 PredicateBuilder 构建表达式树并将其传递到 Linq.Where 中,啦:

IQueryable<Product> SearchProducts (params string[] keywords)
{
  var predicate = PredicateBuilder.False<Product>();

  foreach (string keyword in keywords)
  {
    string temp = keyword;
    predicate = predicate.Or (p => p.Description.Contains (temp));
  }
  return dataContext.Products.Where (predicate);
}

[顺便说一下,这不是我想做的]

有 3 个可选标准,我想写类似于:

 Ratings.Filter = BuildFilterPredicate();  // Ratings = the PagedCollectionView


private Predicate<object> BuildFilterPredicate()
{
  bool FilterOnOrder = !String.IsNullOrEmpty(sOrderNumberFilter);
  var predicate = PredicateBuilder.False<object>();
  if (ViewMineOnly)
  {
    predicate = predicate.And(Rating r => sUserNameFilter == r.Assigned_To);
  }
  if (ViewStarOnly)
  {
    predicate = predicate.And(Rating r => r.Star.HasValue && r.Star.Value > 0);
  }
  if (FilterOnOrder)
  {
    predicate = predicate.And(Rating r => r.ShipmentInvoice.StartsWith(sOrderNumberFilter));
  }
  return predicate;
}

当然,这不会编译,因为 PredicateBuilder 创建一个 Expression> 而不是实际的谓词方法。但我看到有一些方法可以将表达式树转换为方法,因此在我看来应该有一种方法可以完成我所追求的目标,而无需诉诸一堆嵌套的 if/then/else 语句。

所以问题是 - 有没有办法动态构建谓词方法?

TIA

Here's the scenario:
Silverlight 4.0, DataGrid, PagedCollectionView itemssource.
The objective is to apply a Filter to the PCV. The filter needs to be a Predicate<object>(Method) - where Method implements some logic against the object and returns true/false for inclusion.
What I have is a need to optionally include 3 different criteria in the filter logic and explicit code quickly gets ugly. We don't want that, do we?

So I see that there is a way to build an expression tree using PredicateBuilder and pass that into Linq.Where, a la:

IQueryable<Product> SearchProducts (params string[] keywords)
{
  var predicate = PredicateBuilder.False<Product>();

  foreach (string keyword in keywords)
  {
    string temp = keyword;
    predicate = predicate.Or (p => p.Description.Contains (temp));
  }
  return dataContext.Products.Where (predicate);
}

[this is not what I'm trying to do by the way]

With 3 optional criteria, I want to write something like:

 Ratings.Filter = BuildFilterPredicate();  // Ratings = the PagedCollectionView


private Predicate<object> BuildFilterPredicate()
{
  bool FilterOnOrder = !String.IsNullOrEmpty(sOrderNumberFilter);
  var predicate = PredicateBuilder.False<object>();
  if (ViewMineOnly)
  {
    predicate = predicate.And(Rating r => sUserNameFilter == r.Assigned_To);
  }
  if (ViewStarOnly)
  {
    predicate = predicate.And(Rating r => r.Star.HasValue && r.Star.Value > 0);
  }
  if (FilterOnOrder)
  {
    predicate = predicate.And(Rating r => r.ShipmentInvoice.StartsWith(sOrderNumberFilter));
  }
  return predicate;
}

Of course this won't compile because PredicateBuilder creates an Expression<Func<T, bool>> not an actual predicate method. But I see that there are ways to convert an expression tree into a method so it seemed to me there ought to be a way to accomplish what I'm after without resorting to a bunch of nested if/then/else statements.

So the question is - is there a way to build a predicate method dynamically?

TIA

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

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

发布评论

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

评论(3

昔日梦未散 2024-10-01 02:19:46

要对 PagedCollectionView 执行此操作,您需要有一个谓词。所以它看起来像:

private Predicate<object> ConvertExpressionToPredicate(Expression<Func<object, bool>> exp)
{
  Func<object, bool> func = exp.Compile();
  Predicate<object> predicate = new Predicate<object>(func);
  //Predicate<object> predicate = t => func(t);     // also works
  //Predicate<object> predicate = func.Invoke;      // also works
  return predicate;
}

并构建表达式:

private Expression<Func<object, bool>> BuildFilterExpression()
{
  ...snip...
  var predicate = PredicateBuilder.True<object>();
  if (ViewMineOnly)
  {
    predicate = predicate.And(r => ((Rating)r).Assigned_To.Trim().ToUpper() == sUserNameFilter || ((Rating)r).Assigned_To.Trim().ToUpper() == "UNCLAIMED");
  }
  if (ViewStarOnly)
  {
    predicate = predicate.And(r => ((Rating)r).Star.HasValue && ((Rating)r).Star.Value > 0);
  }
  if (FilterOnOrder)
  {
    predicate = predicate.And(r => ((Rating)r).ShipmentInvoice.Trim().ToUpper().StartsWith(sOrderNumberFilter));
  }
  if (ViewDueOnly)
  {
    predicate = predicate.And(r => ((Rating)r).SettlementDueDate <= ThisThursday);
  }
  return predicate;
}

然后设置过滤器:

Ratings.Filter = ConvertExpressionToPredicate(BuildFilterExpression());

to do this for a PagedCollectionView, you need to have a Predicate. So it looks like:

private Predicate<object> ConvertExpressionToPredicate(Expression<Func<object, bool>> exp)
{
  Func<object, bool> func = exp.Compile();
  Predicate<object> predicate = new Predicate<object>(func);
  //Predicate<object> predicate = t => func(t);     // also works
  //Predicate<object> predicate = func.Invoke;      // also works
  return predicate;
}

and build the expression:

private Expression<Func<object, bool>> BuildFilterExpression()
{
  ...snip...
  var predicate = PredicateBuilder.True<object>();
  if (ViewMineOnly)
  {
    predicate = predicate.And(r => ((Rating)r).Assigned_To.Trim().ToUpper() == sUserNameFilter || ((Rating)r).Assigned_To.Trim().ToUpper() == "UNCLAIMED");
  }
  if (ViewStarOnly)
  {
    predicate = predicate.And(r => ((Rating)r).Star.HasValue && ((Rating)r).Star.Value > 0);
  }
  if (FilterOnOrder)
  {
    predicate = predicate.And(r => ((Rating)r).ShipmentInvoice.Trim().ToUpper().StartsWith(sOrderNumberFilter));
  }
  if (ViewDueOnly)
  {
    predicate = predicate.And(r => ((Rating)r).SettlementDueDate <= ThisThursday);
  }
  return predicate;
}

and then set the filter:

Ratings.Filter = ConvertExpressionToPredicate(BuildFilterExpression());
愿得七秒忆 2024-10-01 02:19:46

我遇到了同样的问题。我有 3 个标准。
我所做的如下:

  • 一种验证每个条件的方法
  • 一种验证对象的方法

代码看起来非常干净并且易于维护。

Ratings.Filter = new predicate<objects>(validateObject);

private bool validateObject(object o)
{
  return validateFirstCriteria(o) && 
         validateSecondCriteria(o) && 
         validateThirdCriteria(o);
}

private bool validateFirstObject(object o)
{
  if (ViewMineOnly)
  {
    Rating r = o as Rating;
    if (o != null)
    {
      return  (r.Star.HasValue && r.Star.Value > 0);
    }
  }
  return false;
}
private bool validateSecondObject(object o)
{
  if (ViewStarOnly)
  {
    Rating r = o as Rating;
    if (o != null)
    {
      return sUserNameFilter == r.Assigned_To;
    }
  }
  return false;
}
private bool validateThirdObject(object o)
{
  if (FilterOnOrder)
  {
    Rating r = o as Rating;
    if (o != null)
    {
      return r.ShipmentInvoice.StartsWith(sOrderNumberFilter);
    }
  }
  return false;
}

编辑

如果您想坚持使用表达式树。您可以在这里查看: http://msdn.microsoft.com/en -us/library/bb882536.aspx

您可以将表达式树转换为 lambda 表达式,然后编译 lambda 表达式。之后你就可以使用它作为一种方法。示例:

        // The expression tree to execute.
        BinaryExpression be = Expression.Power(Expression.Constant(2D), Expression.Constant(3D));

        // Create a lambda expression.
        Expression<Func<double>> le = Expression.Lambda<Func<double>>(be);

        // Compile the lambda expression.
        Func<double> compiledExpression = le.Compile();

        // Execute the lambda expression.
        double result = compiledExpression();

        // Display the result.
        Console.WriteLine(result);

        // This code produces the following output:
        // 8

I faced the same problem. I had 3 criteria.
What I did is the following :

  • One method to validate each criteria
  • One method to validate the object

The code looked quite clean and it was easy to maintain.

Ratings.Filter = new predicate<objects>(validateObject);

private bool validateObject(object o)
{
  return validateFirstCriteria(o) && 
         validateSecondCriteria(o) && 
         validateThirdCriteria(o);
}

private bool validateFirstObject(object o)
{
  if (ViewMineOnly)
  {
    Rating r = o as Rating;
    if (o != null)
    {
      return  (r.Star.HasValue && r.Star.Value > 0);
    }
  }
  return false;
}
private bool validateSecondObject(object o)
{
  if (ViewStarOnly)
  {
    Rating r = o as Rating;
    if (o != null)
    {
      return sUserNameFilter == r.Assigned_To;
    }
  }
  return false;
}
private bool validateThirdObject(object o)
{
  if (FilterOnOrder)
  {
    Rating r = o as Rating;
    if (o != null)
    {
      return r.ShipmentInvoice.StartsWith(sOrderNumberFilter);
    }
  }
  return false;
}

EDIT

If you want to stuck to Expression trees. You could take a look here : http://msdn.microsoft.com/en-us/library/bb882536.aspx

You can convert the expression tree to a lambda expression and then compile the lambda expression. After then you can use it as a method. Exemple :

        // The expression tree to execute.
        BinaryExpression be = Expression.Power(Expression.Constant(2D), Expression.Constant(3D));

        // Create a lambda expression.
        Expression<Func<double>> le = Expression.Lambda<Func<double>>(be);

        // Compile the lambda expression.
        Func<double> compiledExpression = le.Compile();

        // Execute the lambda expression.
        double result = compiledExpression();

        // Display the result.
        Console.WriteLine(result);

        // This code produces the following output:
        // 8
风轻花落早 2024-10-01 02:19:46

感谢本杰明的提示和这篇文章 -> 如何转换 Func谓词?
我想通了。

本质是:

private static Predicate<T> ConvertExpressionToPredicate(Expression<Func<T, bool>> exp)
{
  Func<T, bool> func = exp.Compile();
  Predicate<T> predicate = new Predicate<T>(func);
  //Predicate<T> predicate = t => func(t);     // also works
  //Predicate<T> predicate = func.Invoke;      // also works
  return predicate;
}

这会将表达式树编译为单个函数,并返回一个谓词来调用该函数。
在使用中,它看起来像:

private static bool ViewStarOnly;
private static bool LongNameOnly;
static void Main(string[] args)
{
  List<Dabble> data = GetSomeStuff();
  ViewStarOnly = true;
  LongNameOnly = true;
  Expression<Func<Dabble, bool>> exp = BuildFilterExpression();
  List<Dabble> filtered = data.FindAll(ConvertExpressionToPredicate(exp));
  PrintSomeStuff(filtered);
}

private static Predicate<Dabble> ConvertExpressionToPredicate(Expression<Func<Dabble, bool>> exp)
{
  Func<Dabble, bool> func = exp.Compile();
  Predicate<Dabble> predicate = new Predicate<Dabble>(func);
  //Predicate<Dabble> predicate = t => func(t);     // also works
  //Predicate<Dabble> predicate = func.Invoke;      // also works
  return predicate;
}

private static Expression<Func<Dabble, bool>> BuildFilterExpression()
{
  var predicate = PredicateBuilder.True<Dabble>();
  if (ViewStarOnly)
  {
    predicate = predicate.And(r => r.Star.HasValue && r.Star.Value > 0);
  }
  if (LongNameOnly)
  {
    predicate = predicate.And(r => r.Name.Length > 3);
  }
  return predicate;
}

谢谢!

Thanks to Benjamin's hints and this post -> How to convert Func<T, bool> to Predicate<T>?
I figured it out.

The essence is:

private static Predicate<T> ConvertExpressionToPredicate(Expression<Func<T, bool>> exp)
{
  Func<T, bool> func = exp.Compile();
  Predicate<T> predicate = new Predicate<T>(func);
  //Predicate<T> predicate = t => func(t);     // also works
  //Predicate<T> predicate = func.Invoke;      // also works
  return predicate;
}

This will compile the expression tree to a single function and return a predicate to call the function.
In use, it looks like:

private static bool ViewStarOnly;
private static bool LongNameOnly;
static void Main(string[] args)
{
  List<Dabble> data = GetSomeStuff();
  ViewStarOnly = true;
  LongNameOnly = true;
  Expression<Func<Dabble, bool>> exp = BuildFilterExpression();
  List<Dabble> filtered = data.FindAll(ConvertExpressionToPredicate(exp));
  PrintSomeStuff(filtered);
}

private static Predicate<Dabble> ConvertExpressionToPredicate(Expression<Func<Dabble, bool>> exp)
{
  Func<Dabble, bool> func = exp.Compile();
  Predicate<Dabble> predicate = new Predicate<Dabble>(func);
  //Predicate<Dabble> predicate = t => func(t);     // also works
  //Predicate<Dabble> predicate = func.Invoke;      // also works
  return predicate;
}

private static Expression<Func<Dabble, bool>> BuildFilterExpression()
{
  var predicate = PredicateBuilder.True<Dabble>();
  if (ViewStarOnly)
  {
    predicate = predicate.And(r => r.Star.HasValue && r.Star.Value > 0);
  }
  if (LongNameOnly)
  {
    predicate = predicate.And(r => r.Name.Length > 3);
  }
  return predicate;
}

Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文