2009-12-18 8 views
0

Linq에서 SQL로 변환해야하는 SQL 쿼리가 있습니다. 나는 Sums에 올 때까지 성공했고, 나는 거기에 완전히 붙어 있습니다. 나는 단순한/단순한 그룹화와 합계를하는 방법을 알고 있지만 이번에는 필사적으로 도움이 필요합니다. 나는 어떤 생각에 감사 할 것입니다.여러 합, 그룹 및 조인을 사용하는 Linq 쿼리

표 관계는 다음과 같이이다 :

TreeNodes - one-to-many - Dispatches (not every TreeNode has Dispatch entry) 
Dispatches - one-to-many - DispatchesJobs (not every Dispatch has DispatchJob entry) 
DispatchesJobs one-to-many - DispatchesJobsReceivings (not every DispatchJob has DispatchJobReceiving entry) 

기본적으로 내가 무엇을 알아 내야하는 것입니다 : 모든 파견

: 발송의 TreeNode를 이름과 수량을 얻기의에서, 반환 카운트를 보냈다 DispatchesJobsReceivings 테이블입니다.

은 여기 내 SQL 쿼리입니다 : 내가 지금까지 그것을 번역이 왔어요

TreeNodes_id name  quantity returns spent 
------------ ------------ ------- ------- ---------- 
77  CEMENT  20  17  3 
122  SAND   NULL NULL  NULL 

:이 결과를 제공

from t in db.TreeNodes 

join d in db.Dispatches 
on t.TreeNodes_id equals d.TreeNodes_id 

join dj in db.Dispatches_Jobs 
on d.Dispatches_id equals dj.Dispatches_id 
into tmpDJ 
from rowTmpDJ in tmpDJ.DefaultIfEmpty() 

join djr in db.DispatchesJobsReceivings 
on rowTmpDj.DispatchesJobs_id equals djr.DispatchesJobs_id 
into tmpDjr 
from rowTmpDjr in tmpDjr.DefaultIfEmpty() 

select new 
{ 
    TreeNode_id = t.TreeNodes_id, 
    name = t.name, 
    quantity = rowTmpDjr.quantity, 
    returns = rowTmpDjr.returns, 
    spent = rowTmpDjr.spent 
}; 

:

이 결과를 제공

select t.TreeNodes_id, t.name 
sum(djr.quantity) as quantity, sum(djr.returns) as returns, sum(djr.spent) as spent 

from TreeNodes as t 

inner join Dispatches as d 
on t.TreeNodes_id = d.TreeNodes_id 

left outer join DispatchesJobs as dj 
on d.Dispatches_id = dj.Dispatches_id 

left outer join DispatchesJobsReceivings as djr 
on dj.DispatchesJobs_id = djr.DispatchesJobs_id 

group by t.TreeNodes_id, t.name 

TreeNodes_id name  quantity returns spent 
------------ ------------ ------ -------- ---------- 
77   CEMENT   1   0   1 
77   CEMENT   2   0   2 
77   CEMENT   4   4   0 
77   CEMENT   13  13  0 
122   SAND   NULL  NULL  NULL 

그리고 난 여기에서 붙어 있습니다. 나는 어떤 도움을 주셔서 감사합니다.

답변

0

Linqer라는 제품을 봅니다 (관련이 없습니다). T-SQL을 LINQ 쿼리로 변환합니다. 환상적인 도구. 변환 할 수 없었던 쿼리를 실행해야합니다. 30 일간의 시험이 있습니다. 비용은 약 $ 60입니다.

랜디