2014-01-17 3 views
2

다른 Group By의 합계 인 Group By을하려고합니다.다른 그룹의 합계로 그룹화하는 방법?

LINQ에서이 작업을 수행하는 올바른 방법은 무엇입니까?

아래의 쿼리는 작동하지 않지만 달성하고자하는 논리를 보여주기위한 것입니다.

from so in TblServiceOrders 
join sologs in TblSOLogs on so.SONumber equals sologs.SONumber 

where so.DateClosed >= new DateTime(2014,01,17) 
where so.DateClosed <= new DateTime(2014,01,17) 
where sologs.ElapsedHours != 0 || sologs.ElapsedMinutes != 0 

group new {sologs.ElapsedHours, sologs.ElapsedMinutes} by sologs.SONumber into g 
group new {g.Sum (x => x.ElapsedHours), g.Sum (x => x.ElapsedMinutes)} by "Totals" into t 

select new { 
AverageHours = t.Average (x => x.ElapsedHours) 
AverageMins = t.Average (x => x.ElapsedMinutes) 
} 
+0

가능한 중복 http://stackoverflow.com/ 질문/21181696/linq-how-to-show-all-results-in-groupby) – Zache

+1

중복되지 않음, 다른 범위, 다른 질문. –

답변

0

아래에서 작동하는 쿼리가 있습니다. 이제 다음 수수께끼는 VS에서 DataGridView를 채우는 데 사용할 수없는 이유입니다.

작업 쿼리 :-(:

from so in TblServiceOrders 
join sologs in TblSOLogs on so.SONumber equals sologs.SONumber 

where so.DateClosed >= new DateTime(2014,01,17) 
where so.DateClosed <= new DateTime(2014,01,17) 
where sologs.ElapsedHours != 0 || sologs.ElapsedMinutes != 0 

group new {sologs.ElapsedHours, sologs.ElapsedMinutes} by sologs.SONumber into g 
group new {hours = g.Sum (x => x.ElapsedHours), mins = g.Sum (x => x.ElapsedMinutes)} by "Totals" into t 

select new { 
Average = t.Average (x => (x.hours * 60) + x.mins), 
Count = t.Count() 
} 

참고 :

hours = g.Sum (x => x.ElapsedHours), mins = g.Sum (x => x.ElapsedMinutes) 
([GROUPBY 모든 결과의 평균을 표시하는 방법 LINQ]의
관련 문제