Build nested condition with SelectQuery object

非 Y 不嫁゛ 提交于 2019-12-24 04:48:08

问题


Can I build nested conditions using the SelectQuery object?

I want to get:

select * 
from car 
where ((car.color = 'blue' or car.model = 'Genesis') 
or (car.manufactor = 'ford' and car.color = 'blue'))

回答1:


If you inline all conditions / predicates...

... then you can supply them just like that to your SelectQuery object:

SelectQuery query = ...
query.addConditions( ( CAR.COLOR.eq("blue") .or ( CAR.MODEL.eq("Genesis") ) )
                 .or ( CAR.MANUFACTOR.eq("ford") .and ( CAR.COLOR.eq("blue") ) ) )

I've added some whitespace for improved readability

If you want to perform dynamic query building...

... Then you can create your predicate in various steps:

Condition condition;

condition = CAR.COLOR.eq("blue");
condition = condition.or(CAR.MODEL.eq("Genesis"));
condition = condition.or(CAR.MANUFACTOR.eq("ford").and(CAR.COLOR.eq("blue")));

Both approaches are completely equivalent.




回答2:


I'm looking at the documentation now and it looks like you can use the addConditions methods with an OR operator to accomplish your nested query.

There are 4 different overloaded methods with addConditions but the last two allow you to supply an operator (in your case OR) and one of those allows you to string together multiple conditions using a supplied operator.

So all you need to do is break apart your nested statements into separate conditions and then link them together with your addConditions([Collection of Conditions], OR) statement.



来源:https://stackoverflow.com/questions/25894423/build-nested-condition-with-selectquery-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!