Where clause not working with parantheses

2019-09-10 15:28发布

问题:

Suppose the following Query using a NH 3.4 and RepositoryPattern

var list = _repository
     .QueryOver()
     .Where(x => (x.Age > 20))  // notice the parantheses
     .Future()
     .ToList();

Whith these parantheses added the NH is failing to work, and causes a SO exception.

If replacing .Where(x => (x.Age > 20)) with .Where(x => x.Age > 20) it works as expected.

Any clues on why it doesn't work with extra parantheses?

Note

This is a simplified scenario from the bigger picture. In production i'm passing that .Where(...) through a parameter Expression<Func<Person, bool>> where

回答1:

I have a doubt the error is there:

Expression<Func<MyClass, bool>> mc1 = x => (x.ID > 20);
Expression<Func<MyClass, bool>> mc2 = x => x.ID > 20;

var body1 = mc1.Body.NodeType; // GreatThan
var body2 = mc2.Body.NodeType; // GreatThan

The brackets are removed by the compiler. There is nothing in the Expression tree "language" (class system) to explicitly represent a bracket.