3

I have a library that converts a lambda expression to an odata filter string using the ExpressionVisitor class. For example, the expression o => o.Teste == null && !(o.Date != DateTime.Now) turns into $filter=Teste eq null and not(Date ne xx/xx/xx)

It works fine. The problem is when I have to group parts of the condition with parentheses (). The parentheses are ignored and it just writes the condition without it. For example, the expression (o => o.Name != null && o.Name == "Joe") || !(o.Date != DateTime.Now) turns into $filter=Name ne null and Name eq 'Joe' or not(Date ne xx/xx/xx)

What's weird is that the ExpessionVisitor doesn't seem to identify the parentheses, it has a NodeType for not !(expression) but it doesn't have for (expression).

I've been searching for this on Google for two days and I can't seem to find an answer.

Thanks in advance for the Help.

4

1 回答 1

3

not 的节点类型不是因为!(expression)它只是 for !expression- 括号不是它的一部分。

源代码中需要括号来指示您想要的优先级。它们在表达式树中不是必需的,因为优先级是树中固有的。

如果你不介意有时有额外的括号,你可以让你的字符串转换总是包含它们。所以每次你有一个带有操作数表达式xand的 OR or and AND 表达式时y,只需将其转换为(x || y)or (x && y),无条件地包括那些括号。它可能看起来不漂亮,但它总是会给你正确的优先级。

于 2014-07-18T19:28:22.227 回答