How can I construct a complex where object in Subsonic 2.0
I am trying to create a search engine query that looks like this:
SELECT * FROM sometable
WHERE col1 = 1
AND col2 = 2
AND (col3a = 3 OR col3b = 3 OR col3c = 3)
I though the code below worked:
SubSonic.Query query = new SubSonic.Query("sometable");
query = query.WHERE("col1", 1);
query = query.WHERE("col2", 2);
query = query.AND("col3a = " + 3).
OR("col3b = " + 3).
OR("col3c = " + 3);
but this is not the case as the result:
SELECT * FROM sometable
WHERE col1 = 1
AND col2 = 2
AND col3a = 3
OR col3b = 3
OR col3c = 3
How can I build the query I need?
+1
Michal
a source
to share
6 answers
The following should be pretty close to what you want if OpenExpression / CloseExpression is supported in 2.0:
SubSonic.Query query = new SubSonic.Query("sometable");
.WHERE("col1", 1);
.AND("col2", 2);
.AND("col3a = " + 3).
.OpenExpression()
.OR("col3b = " + 3).
.OR("col3c = " + 3);
.CloseExpression()
+3
a source to share
I think you should use WhereExpression / AndExpression / OrExpression to express expressions reading the documentation, but I've never used it so I can't say for sure. Try below and see if it works
SubSonic.Query query = new SubSonic.Query("sometable");
query = query.WHEREEXPRESSION("col1", 1);
query = query.AND("col2 = " + 2);
query = query.ANDEXPRESSION("col3a = " + 3).
OR("col3b = " + 3).
OR("col3c = " + 3);
+2
a source to share