last day (14 days later) » 

11:08
1
A: Grouping Lambda Expressions by Operators and Using Them With DapperExtensions' PredicateGroups

George DuckettYour example lambda, p => p.MarketId == marketId && p.FirstName == "John" || p.FirstName == "Jack" is the equivilent of p => (p.MarketId == marketId && p.FirstName == "John") || p.FirstName == "Jack" because && has a higher precendence than ||. Because of this you'll get a tree with the &&...

Unfortunately I have no control over the lambda being passed in, so I cannot bracket the lambda to change the way it is evaluated as you have shown. Regardless of the operator precedence, I want to be able to parse the lambda from right to left in a recursive manner. BinaryExpression.Right gives me the right-most expression, and I also get the operator to its left so I know its grouping (AND or OR). When I parse the next right-most lambda, I want to place it in the group that the previous lambda belongs to.
So I want to parse the right-most lambdas two at a time with the joining conditional operator. When I get to the point of the left-most lambda, I want to look at its conditional operator to its right and place it in the appropriate group. Hope this makes sense.
Sorry, I don't really follow the general case here. I understand that in this example you want to group the John/Jack tests with the OR operator but I don't understand the general problem you want to solve, are there different rules of operator precedence in your target language for example? Remember that the lambda expression isn't written down, it's a tree structure (as in my answer here). I think what you want is to produce one where && is at the top and Jack and John are on the right with the ||, but I don't know why you want that, or what else you'd want with different inputs.
Could you look at the tree structure in my answer and know what output you want, knowing nothing of how the expression is written in the first place? (because the code doesn't see how it's written)
For the moment, assume there is no operator precedence. && has the same precedence as ||. The issue is now how to parse the overall lambda two expressions at a time, from right to left (as mentioned before, BinaryExpression.Right gives me the right-most expression, while BinaryExpression.NodeType gives me the operator to its left). Once that has been solved, we can consider operator precedence.
My question then is, why do you want to parse it from right to left? It looks like you're trying to create 2 predicate groups, one for the ands and one for the ors to which should be logically equivilent. While that might work in your example case it won't in the general case. Consider (a && b) || (c && d) What would you do then? I think what should happen is that each and and each or should translate into its own predicate group. See the "Multiple Compound Predicates (Predicate Group)" section of your linked article. You'd simply replace BinaryExpression with a predicate group.
Under the left or right of that predicate group you might have another predicate group.
Okay, for your example: (a && b) || (c && d) - that would be two AND predicate group with two predicates each, and the overall predicate group passed to Dapper would be of type OR
I totally understand why this is getting confusing. I need to create a predicate group containing a maximum of two predicates, for each pair of expressions in the overall lambda.
11:12
right, ok, I understand now. There no reason to parse from right to left though.
Well, how would I get the leftmost expression in C#, recursively?
In your example you have (a && b) || c with the || at the top. What you want to end up with is each time you have an operator you want to create a predicate group, with the left and right as the list of expressions.
In your logic to convert a binaryexpression to a predicate group you would first use that same function to convert the left and the right into a predicate group
i.e. your code sees the ||.
it creates a predicate group ready to add the expressions to its list
it chooses left first (doesn't matter).
ok, left is another binary expression, so it calls its self to get a predicategroup that your target library understands
so that recursion is working on only a && b. It chooses left first, sees a simple predicate, a which means it can simply add it to the predicate group, it does the same for the right
then we're back up to the orignal call to the function which now has a predicate group with the lower expression on the left converted to a different predicate group and added.
it goes down the right now, which is simple an individual predicate and so it can add it to its list.
And its list contains just c?
its list contains 1) the predicate group we created for a && b2) c
1) the predicate group we created for a && b2) c
grr, formatting 1 sec.
1) the predicate group we created for a && b
2) c
Hmmm there is a light dawning on me here
11:20
If you imagine the whole expression as a tree as in my example imagine replacing the binaryexpressions with predicategroup
but it still sort of looks like a tree
instead of left and right making the branches, the list of predicates makes the branches
the result is the same
I wonder why BinaryExpression.Right returns a relatively simple expression while BinaryExpression.Left returns whatever's left ... To me it would make more sense for it to be the other way around
That is what has been tripping me up
it's because of the operator precedence and they way the sub-expressions are evaluated
i.e. to calculate a && b || c you first do a && b, then || that with c
Okay, so if I had something crazy like: a && b || c || d && e
I know that BinaryExpression.NodeType would be ExpressionType.AndAlso
BinaryExpression.Right would be e
BinaryExpression.Left would be a && b || c || d
Would you mind going through that one? I want to understand this completely before going back to code
Ok, taking into account higher precidence we get the following:
((a && b) || c) || (d && e)
note that i'm not 100% sure about either the bracket would put c with the first && or the last, but it doens't matter, the logic is the same
When visualising the tree, start with the inner most brackets. They are the "leaves"
then work outwards, using the brackets to work up the tree, finally arriving at the root node, which in our case is the || to the right of c
||
/ \
|| &&
/ \ / \
&& c d e
/ \
a b
          ||
        /    \
       ||     &&
      /  \   /  \
     &&   c d   e
    /  \
   a    b
11:38
Alright, thanks I think I get it. Will write out the recursive method in code and get back to you. Thanks a lot George! :)

last day (14 days later) »