Subsonic Query (ConditionA OR ConditionB) AND ConditionC

♀尐吖头ヾ 提交于 2019-12-06 07:56:42

问题


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);

回答1:


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/




回答2:


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.




回答3:


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>();



回答4:


If you already use SubSonic3, with a linq query this is quite easy:

var result = from c in db.Challenges
             where (c.ChallengeeKey == playerKey || c.ChallengerKey == playerKey)
                 && c.Complete == false
             select c;

with the query tool (as mentioned by others) OrExpression / CloseExpression is the right way to generate the right query for your.



来源:https://stackoverflow.com/questions/765896/subsonic-query-conditiona-or-conditionb-and-conditionc

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