tags:

views:

1239

answers:

3

How do I build a query in Subsonic that of this format

(ConditionA OR ConditionB) AND ConditionC

Iv tried various approaches but I cant seem to get the desired result.

Here is one thing i tired:

Query q = Challenge.CreateQuery();
      q.WHERE(Challenge.Columns.ChallengeeKey, playerKey)
      .OR(Challenge.Columns.ChallengerKey, playerKey);
       q.AND(Challenge.Columns.Complete, false);
+2  A: 

If I'm not wrong, this is a Subsonic "feature" with OR.

Refactor your query as

(ConditionA AND ConditionC) OR (ConditionB AND ConditionC)

In this case your Subsonic query like

q.WHERE(...).AND(...).OR(...).AND(...)

Edit:

Find some interresing thing here. The main idea is using the

CloseExpression()

tag.

boj
+5  A: 

If you use 2.2 (or 2.1) you can open up expressions:

Northwind.ProductCollection products = new Select(Northwind.Product.Schema)
    .WhereExpression("categoryID").IsEqualTo(5).And("productid").IsGreaterThan(10)
    .OrExpression("categoryID").IsEqualTo(2).And("productID").IsBetweenAnd(2, 5)
    .ExecuteAsCollection<Northwind.ProductCollection>();

You can read a bit more here: http://blog.wekeroad.com/subsonic/subsonic-version-21-pakala-preview-the-new-query-tool/

Rob Conery
Am I right in saying this doesn't specifically address the problem? This is more syntactic sugar?
Dan
The question - I think - is about how to wrap in parens for an expression - this is a sample query on how to do it.
Rob Conery
Arr! Spot on, thanks rob
Dan
+1  A: 

I'm using Subsonic 2.2, I tried a few variations on Rob's example but kept getting an exception with the message: "Need to have at least one From table specified"

In the end this achieved the desired result:

          Challenge challenge = new Select().From(Challenge.Schema)
           .WhereExpression(Challenge.Columns.ChallengerKey).IsEqualTo(playerKey)
           .Or(Challenge.Columns.ChallengerKey).IsGreaterThan(playerKey)
           .AndExpression(Challenge.Columns.Complete).IsEqualTo(false)
           .ExecuteSingle<Challenge>();
Dan