使用左外连接将Oracle SQL查询转换为Linq

时间:2017-08-02 11:54:59

标签: linq

我正在尝试将下面的Oracle Sql查询转换为linq,但没有取得多大成功。我不知道如何处理行AND ShipSeq = i.ShipSeq(+),我在Oracle中学到的是LEFT OUTER JOIN。我正在LinqPad中测试下面的Linq查询,我没有得到任何语法错误,但在执行查询时出错。有什么想法吗?

Oracle Sql查询

SELECT *
FROM   CustomerShip,
    (SELECT DISTINCT b.ShipSeq AS shipSeq
     FROM   Orders a,
            CustomerShip b
     WHERE  a.OrderId IN (SELECT OrderId
                          FROM   Orders
                          WHERE  CustomerId = @CustomerId
                          AND    OrderType <> 'A')
     AND    b.CustomerId = @CustomerId
     AND    b.ShipSeq = a.CustShip
     AND    OrderStatus <> 'C'
     GROUP BY b.ShipSeq) i
WHERE  CustomerId = @CustomerId
AND    (Address NOT LIKE '%RETAIL%STORE%')
AND    ShipSeq = i.ShipSeq(+)
ORDER BY ShipTo DESC, OrderDate DESC;

Linq查询

var query = from s in CustomerShip
            join m in Orders on s.ShipTo equals m.ShipTo into temp
            from x in temp.DefaultIfEmpty()
            where (from o in Orders
                   from c in CustomerShip
                   where (from x in CustomerOrders
                          where x.CustomerId == customerId
                          && !x.OrderType.Equals("A")
                          select x.OrderId).Contains(o.OrderId)
                     && c.CustomerId == customerId
                     && c.ShipTo == o.ShipTo
                     && !o.OrderStatus.Equals("C")
                     select c.ShipTo).Distinct().Contains(s.ShipTo)
            && s.CustomerId == customerId
            && !s.Address.Contains("RETAIL")
            && !s.Address.Contains("STORE")
            orderby s.ShipTo descending, s.OrderDate descending
            select s;

1 个答案:

答案 0 :(得分:0)

我认为字面翻译看起来像这样:

   signInFacebookLoginInFirebase(facebookToken){
     const credential = Fb.firebase.auth.FacebookAuthProvider.credential(facebookToken);
     Fb.firebase
       .auth()
       .signInWithCredential(credential)
       .then(() => alert('Account accepted'))
       .catch((error) => alert('Account disabled'));
   }

但SQL和LINQ对我来说效率都不高,但如果没有领域知识,这就是我可以优化的全部内容:

var iQuerySub = from o in Orders where o.CustomerId == pCustomerId && o.OrderType != "A" select o.OrderId;

var iQuery = (from a in Orders
              from b in CustomerShip
              where iQuerySub.Contains(a.OrderId) &&
              b.CustomerId == pCustomerId &&
              b.ShipSeq == a.CustShip &&
              a.OrderStatus != "C"
              group b by b.ShipSeq into bg
              select new { shipSeq = bg.Key }).Distinct();

var ans = from s in CustomerShip
          where s.CustomerId == pCustomerId &&
          (!s.Address.Contains("RETAIL") || !s.Address.Contains("STORE") || s.Address.IndexOf("RETAIL") > s.Address.IndexOf("STORE"))
          join i in iQuery on s.ShipSeq equals i.shipSeq into ij
          from i in ij.DefaultIfEmpty()
          orderby s.ShipTo, s.OrderDate descending
          select new { s, shipSeq = (i != null ? i.shipSeq : (int?)null) };