如何将此查询转换为LINQ?

时间:2016-07-28 08:36:52

标签: linq

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;

此处ProcedureLookupTreatmentsPatientProcedures是表格。

1 个答案:

答案 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();