2

I have two expressions:

public static Expression<Func<int, bool>> IsDivisibleByFive() {
   return (x) => x % 5 == 0;
}

and

public static Expression<Func<int, bool>> StartsWithOne() {
   return (x) => x.ToString().StartsWith("1");
}

And I want to create a new expression that applies both at once (the same expressions are used all over my code in different combinations):

public static Expression<Func<int, bool>> IsValidExpression() {
   return (x) => IsDivisibleByFive(x) && StartsWithOne(x);
}

Then do:

public static class IntegerExtensions
{
    public static bool IsValid(this int self) 
    {
        return IsValidExpression().Compile()(self);
    }
}

And in my code:

if (32.IsValid()) {
   //do something
}

I have many such expressions that I want to define once instead of duplicating code all over the place.

Thanks.

Ryan Weir
  • 6,377
  • 5
  • 40
  • 60
  • 2
    Why are you working with expressions in the first place and not just delegates? – svick Jan 12 '12 at 19:33
  • Added more details, but basically my third code block doesn't actually work, that's the 'theoretical how I wish I could do it'. Right now I must manually create a new method to return an expression that's the combination of those first two, and then I must maintain both separately in my code base. – Ryan Weir Jan 12 '12 at 19:33
  • 1
    This is a duplicate from this other post: [Combining two expressions][1] [1]: http://stackoverflow.com/questions/457316/combining-two-expressions-expressionfunct-bool – Xtian Macedo Jan 12 '12 at 19:34
  • @svick: I want to leave them expressions so that linq-to-sql can also translate them for execution against the database. The idea is I define the expression once, and then I can choose to use most of the same ones for database queries and linq-to-objects as well. – Ryan Weir Jan 12 '12 at 19:35
  • Does this answer your question? [Expression> as Property](https://stackoverflow.com/questions/56663177/expressionfuncobject-bool-as-property) – d219 Jul 24 '20 at 12:41

1 Answers1

6

The problem you'll run into if you just try combining the expression bodies with an AndAlso expression is that the x parameter expressions are actually two different parameters (even though they have the same name). In order to do this, you would need to use an expression tree visitor to replace the x in the two expressions you want to combine with a single, common ParameterExpression.

You may want to look at Joe Albahari's PredicateBuilder library, which does the heavy lifting for you. The result should look something like:

public static Expression<Func<int, bool>> IsValidExpression() {
   return IsDivisibleByFive().And(StartsWithOne());
}
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315