简体   繁体   English

亚音速查询(ConditionA OR ConditionB)和ConditionC

[英]Subsonic Query (ConditionA OR ConditionB) AND ConditionC

How do I build a query in Subsonic that of this format 如何在Subsonic中构建此格式的查询

(ConditionA OR ConditionB) AND ConditionC (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);

If you use 2.2 (or 2.1) you can open up expressions: 如果你使用2.2(或2.1),你可以打开表达式:

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/ 你可以在这里阅读更多内容: http//blog.wekeroad.com/subsonic/subsonic-version-21-pakala-preview-the-new-query-tool/

If I'm not wrong, this is a Subsonic "feature" with OR. 如果我没错,这是带有OR的亚音速“功能”。

Refactor your query as 将您的查询重构为

(ConditionA AND ConditionC) OR (ConditionB AND ConditionC)

In this case your Subsonic query like 在这种情况下,您的Subsonic查询就像

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

Edit: 编辑:

Find some interresing thing here . 在这里找一些有趣的事情。 The main idea is using the 主要的想法是使用

CloseExpression()

tag. 标签。

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" 我正在使用Subsonic 2.2,我在Rob的例子中尝试了一些变化但是仍然得到一条例外消息:“需要至少指定一个From表”

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

If you already use SubSonic3, with a linq query this is quite easy: 如果你已经使用了带有linq查询的SubSonic3,这很容易:

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. 使用查询工具(如其他人所述)OrExpression / CloseExpression是为您生成正确查询的正确方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM