views:

347

answers:

6

I'm trying to build a query to use for a search engine, that would look like this one:

SELECT * FROM sometable

WHERE col1 = 1 AND col2 = 2 AND (col3a = 3 OR col3b = 3 OR col3c = 3)

I though the code below would work:

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 it doesn't as it results in something like this:

SELECT * FROM sometable
WHERE col1 = 1
AND   col2 = 2
AND   col3a = 3
OR    col3b = 3
OR    col3c = 3

How can I build a query I need?

+1  A: 

You can create two one query first that include all OR's after that from the resulting view..you can filter out AND condition..I dint check it out..but hope it will work...

sona
Can you provide some example code?
i dont know SubSonic...So i cant give you example...i suggested with the knowledge of SQL...
sona
Try thisdeclare @temp(column names u specify here)insert into @temp(column names)(select * from Table1 where col3a = 3 OR col3b = 3 OR col3c = 3)select * from @temp where col1 = 1 AND col2 = 2Dont know whether temp will work in SubSonic or not..just try
sona
+1  A: 

I don't know SubSonic, but would this work?

query = query.AND("col3a = " + 3 + " OR col3b = " + 3 + " OR col3c = " + 3);

You could easily build that substring programmatically.

JimG
No, it doesn't work
+2  A: 

I think you are meant to use WhereExpression/AndExpression/OrExpression to nest expressions reading the documentation but Ive never used it so cant say for sure. Try the 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);
Trotts
Thanks, the version I'm using (v2.0.50727) doesn't have WHEREEXPRESSION or ANDEXPRESSION and I can't use new version for now.
There was also mention of a OpenExpression and CloseExpression methods but couldnt find any examples of usage so I am just assuming they do what is implied by the name. Would those be available to you?
Trotts
Unfortunately not
+2  A: 

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()
Adam
not supported :(
In that case, if upgrading really isn't an option, then you're probably best to either use an inline query or a view.
Adam
A: 

I see it's not as simple as I expected, at least not in the version I have so I decided to loop through the DataSet manually to filter out the records from the last check. Unless there is a better way?

A: 

I would recommend you to upgrade to SubSonic 2.2... The new query functions added in 2.1 are much more powerful.

Johan Kronberg