如何按另一组的总和分组?

时间:2014-01-17 11:20:06

标签: c# linq

我正在尝试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)
}

1 个答案:

答案 0 :(得分: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)