I am working on converting an Oracle Sql query to Linq and do not know how to proceed. Here is the Sql request:
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;
I tried splitting it into three separate queries when converting to linq.
var query1 = from c in CustomerShip
where c.CustomerId == customerId
&& !c.Address.Contains("RETAIL")
&& !c.Address.Contains("STORE")
orderby c.ShipTo descending, c.OrderDate descending
select c;
var query2 = from o in Orders
where o.CustomerId == customerId
&& !o.OrderType.Equals("A")
select o.OrderId;
var query3 = (from o in Orders
from c in CustomerShip
where c.CustomerId == customerId
&& c.ShipSeq == o.CustShip
&& !o.OrderStatus.Equals("A")
select c.ShipSeq).Distinct();
Now I'm trying to collect them all in one request, but I don’t know how to do it. Here is the direction I'm going in:
var query = from c in CustomerShip
let subquery = from o in Orders
where o.CustomerId == customerId
&& !o.OrderType.Equals("A")
select o.OrderId
from or in model.Orders
where subquery.Contains(or.OrderId)
&& c.CustomerId == customerId
&& c.ShipSeq == or.CustShip
&& !or.OrderStatus.Equals("A")
group c by c.ShipSeq
into i
select c.ShipSeq
where c.CustomerId == customerId
&& !c.Address.Contains("RETAIL")
&& !c.Address.Contains("STORE")
orderby c.ShipTo descending, c.OrderDate descending
select c, i;
UPDATE
I have a query that executes types, but it takes almost two minutes to execute (compared to 0.02 for an Oracle query), and the order of the results is incorrect. Does anyone see what I'm missing?
var innerQuery = from x in model.Orders
where x.CustomerId == customerId
&& !x.OrderType.Equals("A")
select x.OrderId;
var result = from c in model.CustomerShip
join subQuery in
(
(from o in model.Orders
from c in model.CustomerShip
where c.CustomerId == customerId
&& innerQuery.Contains(o.OrderId)
&& !o.FLAG_ORD_STATUS.Equals("C")
&& c.ShipSeq == o.CustShip
select c.ShipSeq).Distinct()
) on c.ShipSeq equals subQuery into temp
from x in temp.DefaultIfEmpty()
where c.CustomerId == customerId
&& !c.Address.Contains("RETAIL")
&& !c.Address.Contains("STORE")
orderby c.ShipTo descending, c.OrderDate descending
select c;
source
share