2016-07-28 149 views
-2
SELECT * 
FROM 
    ProcedureLookup ProcLookup 
    INNER JOIN (SELECT PatientProcedures.PP_ProcedureId, COUNT(*) as ProcCount 
       FROM (PatientProcedures INNER JOIN Treatments ON PatientProcedures.PP_TreatmentId = Treatments.TS_TreatmentId) 
       WHERE YEAR(Treatments.TS_Date) = YEAR(GETDATE()) 
       GROUP BY PatientProcedures.PP_ProcedureId) cyearProc ON ProcLookup.PL_ProcedureId = cyearProc.PP_ProcedureId 
ORDER BY ProcCount DESC; 

这里ProcedureLookup,Treatments,PatientProcedures是表格。如何将此查询转换为LINQ?

+0

你有没有尝试过任何东西?你能分享你的代码吗? –

回答

0

这里linq查询。

var result = (from ProcLookup in db.ProcedureLookup 
      join cyearProc in (
            from p in db.PatientProcedures 
            join t in db.Treatments on p.PP_TreatmentId equals 
                  t.TS_TreatmentId 
            where 
            t.TS_Date.Year == DateTime.Now.Year 
            group p by p.PP_ProcedureId into g 
            select new 
            { 
            PP_ProcedureId = g.Key, 
            ProcCount = g.Count() 
            } 
           ) on ProcLookup.PL_ProcedureId equals 
            cyearProc.PP_ProcedureId 
      orderby cyearProc.ProcCount descending 
      select new 
      { 
       // Columns 
       PP_ProcedureId = ProcLookup.PP_ProcedureId, 
       ProcCount = cyearProc.ProcCount 
      }).ToList(); 
+1

当我做出一些更改它的作品谢谢你宝贵的时间 – aNTONY