合并两个选择语句

时间:2015-06-15 11:23:45

标签: sql database select where

我有两个陈述:

var chgAssociationQuery1 = (from a in sostenuto.PROBLEMS
  join b in sostenuto.S_ASSOCIATION on a.SERVICEREQNO equals b.FROMSERVICEREQNO
  join c in sostenuto.Changes on b.TOSERVICEREQNO equals c.SERVICEREQNO
  where b.FROMSERVICEID == 101001110
     && b.TOSERVICEID == 101001109
     && a.NAME.Contains(name)
  select new { ProblemReqNo = a.SERVICEREQNO,
                  ProblemId = a.SERVICEREQID, 
                ChangeReqNo = c.SERVICEREQNO, 
                   ChangeId = c.SERVICEREQID,
               ChangeNumber = c.CSEBRANCHCHANGE,
               ChangeBranch = c.MAINTBRANCH});

var chgAssociationQuery2 = (from a in sostenuto.PROBLEMS
  join b in sostenuto.S_ASSOCIATION on a.SERVICEREQNO equals b.TOSERVICEREQNO
  join c in sostenuto.Changes on b.FROMSERVICEREQNO equals c.SERVICEREQNO
  where b.FROMSERVICEID == 101001109
     && b.TOSERVICEID == 101001110
     && a.NAME.Contains(name)
  select new { ProblemReqNo = a.SERVICEREQNO,
                  ProblemId = a.SERVICEREQID,
                ChangeReqNo = c.SERVICEREQNO,
                   ChangeId = c.SERVICEREQID,
               ChangeNumber = c.CSEBRANCHCHANGE,
               ChangeBranch = c.MAINTBRANCH });

var vProblemxChange = chgAssociationQuery1.Union(from a in chgAssociationQuery2 select a);

我希望能够将它们组合成一个;因为它目前在数据库中循环两次 - 这是非常低效的。我已经尝试过研究如何在where子句中放入多个select语句,但我似乎无法找到任何我能理解的内容。

任何人都可以了解我的情况吗?

我尝试修改以下答案:

        var vProblemxChange =
      (from a in sostenuto.PROBLEMS
       join b in sostenuto.S_ASSOCIATION on a.SERVICEREQNO equals b.FROMSERVICEREQNO
       join c in sostenuto.Changes on b.TOSERVICEREQNO equals c.SERVICEREQNO
       join z in sostenuto.S_ASSOCIATION on a.SERVICEREQNO equals z.TOSERVICEREQNO
       join y in sostenuto.Changes on z.FROMSERVICEREQNO equals y.SERVICEREQNO
       where ((b.FROMSERVICEID == 101001110 && b.TOSERVICEID == 101001109 && a.NAME.Contains(name)) ||
              (z.FROMSERVICEID == 101001109 && z.TOSERVICEID == 101001110) && a.NAME.Contains(name))
          && a.NAME.Contains(name)
       select new
       {
           ProblemReqNo = a.SERVICEREQNO,
           ProblemId = a.SERVICEREQID,
           ChangeReqNo = c.SERVICEREQNO,
           ChangeId = c.SERVICEREQID,
           ChangeNumber = c.CSEBRANCHCHANGE,
           ChangeBranch = c.MAINTBRANCH
       });

然而,当我尝试基于where子句访问不同的连接时,我无法在select语句中更改它;例如,如果OR的后半部分为真,则应该在select而不是a和c中使用a和y。

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

var chgAssociationQuery1 = 
      (from a in sostenuto.PROBLEMS
       join b in sostenuto.S_ASSOCIATION on a.SERVICEREQNO equals b.FROMSERVICEREQNO
       join c in sostenuto.Changes on b.TOSERVICEREQNO equals c.SERVICEREQNO
       where ((b.FROMSERVICEID == 101001110 && b.TOSERVICEID == 101001109) ||
              (b.FROMSERVICEID == 101001109 && b.TOSERVICEID == 101001110))
          && a.NAME.Contains(name)
       select new { 
                      ProblemReqNo = a.SERVICEREQNO, 
                      ProblemId = a.SERVICEREQID, 
                      ChangeReqNo = c.SERVICEREQNO, 
                      ChangeId = c.SERVICEREQID, 
                      ChangeNumber = c.CSEBRANCHCHANGE, 
                      ChangeBranch = c.MAINTBRANCH
                  }).Distinct();

答案 1 :(得分:1)

尝试这个

var chgAssociationQuery1 = ((from a in sostenuto.PROBLEMS
                                         join b in sostenuto.S_ASSOCIATION on a.SERVICEREQNO equals b.FROMSERVICEREQNO
                                         join c in sostenuto.Changes on b.TOSERVICEREQNO equals c.SERVICEREQNO
                                         where b.FROMSERVICEID == 101001110
                                               && b.TOSERVICEID == 101001109
                                               && a.NAME.Contains(name)
                                         select new { ProblemReqNo = a.SERVICEREQNO, ProblemId = a.SERVICEREQID, ChangeReqNo = c.SERVICEREQNO, ChangeId = c.SERVICEREQID, ChangeNumber = c.CSEBRANCHCHANGE, ChangeBranch = c.MAINTBRANCH})
                                       .Union(from a in sostenuto.PROBLEMS
                                         join b in sostenuto.S_ASSOCIATION on a.SERVICEREQNO equals b.TOSERVICEREQNO
                                         join c in sostenuto.Changes on b.FROMSERVICEREQNO equals c.SERVICEREQNO
                                         where b.FROMSERVICEID == 101001109
                                               && b.TOSERVICEID == 101001110
                                               && a.NAME.Contains(name)
                                            select new { ProblemReqNo = a.SERVICEREQNO, ProblemId = a.SERVICEREQID, ChangeReqNo = c.SERVICEREQNO, ChangeId = c.SERVICEREQID, ChangeNumber = c.CSEBRANCHCHANGE, ChangeBranch = c.MAINTBRANCH })).ToList();