14

我想组合两个 Linq 表达式的结果。它们以形式存在

Expression<Func<T, bool>>

所以我想要组合的两个本质上是一个参数(类型 T)的委托,它们都返回一个布尔值。我想要组成的结果将是布尔值的逻辑评估。我可能会将其实现为扩展方法,因此我的语法类似于:

Expression<Func<User, bool>> expression1 = t => t.Name == "steve";
Expression<Func<User, bool>> expression2 = t => t.Age == 28;
Expression<Func<User, bool>> composedExpression = expression1.And(expression2);

稍后在我的代码中,我想评估组合表达式

var user = new User();
bool evaluated = composedExpression.Compile().Invoke(user);

我已经提出了一些不同的想法,但我担心它比我希望的要复杂。这是怎么做到的?

4

1 回答 1

20

这是一个例子:

var user1 = new User {Name = "steve", Age = 28};
var user2 = new User {Name = "foobar", Age = 28};

Expression<Func<User, bool>> expression1 = t => t.Name == "steve";
Expression<Func<User, bool>> expression2 = t => t.Age == 28;

var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());

var result = Expression.Lambda<Func<User, bool>>(Expression.And(expression1.Body, invokedExpression), expression1.Parameters);

Console.WriteLine(result.Compile().Invoke(user1)); // true
Console.WriteLine(result.Compile().Invoke(user2)); // false

您可以通过扩展方法重用此代码:

class User
{
  public string Name { get; set; }
  public int Age { get; set; }
}

public static class PredicateExtensions
{
  public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expression1,Expression<Func<T, bool>> expression2)
  {
    InvocationExpression invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());

    return Expression.Lambda<Func<T, bool>>(Expression.And(expression1.Body, invokedExpression), expression1.Parameters);
  }
}

class Program
{
  static void Main(string[] args)
  {
    var user1 = new User {Name = "steve", Age = 28};
    var user2 = new User {Name = "foobar", Age = 28};

    Expression<Func<User, bool>> expression1 = t => t.Name == "steve";
    Expression<Func<User, bool>> expression2 = t => t.Age == 28;

    var result = expression1.And(expression2);

    Console.WriteLine(result.Compile().Invoke(user1));
    Console.WriteLine(result.Compile().Invoke(user2));
  }
}
于 2008-09-10T08:38:22.883 回答