2017-06-22 53 views
0

如何通過多列組來自不同表的到來,終於總結量列如何通過多列組聯接使用LINQ

 var result = _employeeDAL.GetList(db => 
         from es in db.Set<EmployeeSchedule>() 
         join est in db.Set<EmployeeSalaryType>() on est.Employee_Id equals es.Employee_Id 
         join et in db.Set<EmployeeType>() on est.Employee_Salary_Id equals et.Employee_Salary_Id 
         where es.Status == "JOINED" && es.JoinedDate.Year == 2017 
         select new 
         { 
          EmployeeName = es.EmployeeName 
          ,Amount = est.Amount ///----------> SUM THIS 
          ,EmployeeType = et.EmployeeType 
         }); 

這裏就是我試圖

 var result = _employeeDAL.GetList(db => 
         from es in db.Set<EmployeeSchedule>() 
         join est in db.Set<EmployeeSalaryType>() on est.Employee_Id equals es.Employee_Id 
         join et in db.Set<EmployeeType>() on est.Employee_Salary_Id equals et.Employee_Salary_Id 
         group new { es, est, et } by new { es.EmployeeName, et.EmployeeType } into grp 
         where es.Status == "JOINED" && es.JoinedDate.Year == 2017 
         select new 
         { 
          EmployeeName = es.EmployeeName 
          ,Amount = grp.Sum(g=>g.est.Amount) 
          ,EmployeeType = et.EmployeeType 
         }); 
+0

那麼,是什麼問題? – octavioccl

+0

導致超時,所以只是檢查是否有其他優化的方法來拉大數據 – Kiran

回答

2

你是差不多了。只需要考慮到在group ... by ... into grp條款之後,範圍中唯一的變量是grp

group by之前那麼移動where條款,並使用grp.Keyselect

var result = _employeeDAL.GetList(db => 
    from es in db.Set<EmployeeSchedule>() 
    join est in db.Set<EmployeeSalaryType>() on est.Employee_Id equals es.Employee_Id 
    join et in db.Set<EmployeeType>() on est.Employee_Salary_Id equals et.Employee_Salary_Id 
    where es.Status == "JOINED" && es.JoinedDate.Year == 2017 
    group new { es, est, et } by new { es.EmployeeName, et.EmployeeType } into grp 
    select new 
    { 
     EmployeeName = grp.Key.EmployeeName, 
     Amount = grp.Sum(g => g.est.Amount), 
     EmployeeType = grp.Key.EmployeeType, 
    });