2017-04-01 91 views
-2

查詢:加入,group by和SQL計數到LINQ轉換

SELECT COUNT(*) as Avalied 
FROM LeaveMaster as lm 
JOIN LeaveSubsidary as ls 
ON lm.TransMasterId = ls.TransMasterId 
WHERE lm.SystemCode = 'abc123' AND lm.EmployeeCode = '0014' AND lm.Status ='Approved' 
Group by ls.Leave_Type 

你會如何轉換上面的SQL查詢LINQ?謝謝:)

編輯:

from p in db.PY_LeaveTransactionMasterTAB.AsEnumerable() 
join e in db.PY_LeaveTransactionSubsidaryTAB.AsEnumerable() on p.LV_TransMasterId equals e.LV_TransMasterId into grp 
where p.SystemCode == SysCode && p.EmployeeCode == EmpCode && p.Status == "Approved" 
group grp by e.Leave_Type // This line is invalid Why? 

以上是我到目前爲止已經試過,我爲什麼不能做e.Leave_Type或什麼是做group grp by e.Leave_Type正確的方法是什麼?

+0

可能重複的[LINQ - 左加入,集團通過和Count(http://stackoverflow.com/questions/695506/linq-left-join-group by-and-count) – GurV

+0

@GurV我已經看過那個鏈接,看看我編輯的部分。 –

回答

1

你可以試試這個:

(from p in db.PY_LeaveTransactionMasterTAB 
join e in db.PY_LeaveTransactionSubsidaryTAB on p.LV_TransMasterId equals e.LV_TransMasterId 
where p.SystemCode == SysCode && p.EmployeeCode == EmpCode && p.Status == "Approved" 
group new {p,e} by new {e.Leave_Type } into gr 
select new 
{ 
    Leave_Type = gr.Key.Leave_Type , 
    Count = gr.Count() 
}).ToList(); 
+0

這就是答案! –