Hi, I am writing my linq-provider for one CMS. using ExpressionVisitor, I try to parse the expression tree, and depending on the expressions encountered, do something. The problem arose with this. Suppose there are 5 examples (they are meaningless in themselves, given purely for understanding the problem):

1. x=> x.IsBool == true 2. x=> x.IsBool != true 3. x=> x.IsBool 4. x=> !x.IsBool 5. x=> x.IsBool && x.IsBool == true 

The task: you need to turn each BinaryExpression into IsBool = 1/0 (as EntityFramework does).

Cases 1 and 2 are easy to understand. But what about those cases where part of the expression is simplified (redundant boolean expression)? Case 3 just goes as MemberExpression. Case 4 is similar, unless it is wrapped in UnaryExpression (here you can dodge the condition that it is UnaryExpression that contains MemberExpression - then we define our equality).

Case 5 is the most incomprehensible to me. The visitor will visit MemberExpression 2 times (left side x.IsBool, and right side from BinaryExpression x.IsBool == true). In this case, if you just define MemberExpression by the type of the boolean value, then at the output I will get something like

 IsBool = 1 AND IsBool = 1 = 1 

That is, in one iteration I define by the normal path that the right-hand side is BinaryExpression, and then also in MemberExpression I define by the type bool and I deduce the unnecessary.

I hope I explained the essence of the problem. I hope that is not very confusing :). I would be grateful for the help.

  • The case of a unary expression is simple: check the type (bool) and add implicitly == true . - VladD
  • For case 5, in the same way, turn into x.IsBool == true && x.IsBool == true , and x.IsBool == true && x.IsBool == true over with the simplifier of expressions: E && E -> E - VladD
  • Vlad, and that's where the space is - how to determine that (take case 5), when visiting MemberExpression for the first time, we see a shortened comrade, and in the second visit of the right, we have quite a complete comparison? The visitor visits all members of the expression. I can naturally define it, but in the second case, where the expression is complete - I will add the redundant part, since the member came from BinaryExpression - DolceVita
  • I also want to add that ExpressionVisitor only burns a unary expression in case 4. In cases 3 and 5, he does not see it (as a positive unary expression). In case 3, he simply perceives it as MemberExpression. Case 5 - Member And Binary (Member equals Constant). Hence the problem conversion IsBool = 1 AND IsBool = 1 = 1 - DolceVita
  • Well, make a few passes. One pass - replace unary boolean expressions that are not part of the comparison with binary ones. Pass in the comparison flag that the previous level comparison is binary, for example. And do the same for MemberExpression, of course, not only for unary. Or something more cunning. - VladD

0