简体   繁体   English

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

[英]How to Group by the sum of another Group by?

I am trying to do a Group By the sum of another Group By . 我试图做一个Group By与另一个Group By的总和。

What is the correct way to do this in LINQ ? 在LINQ中执行此操作的正确方法是什么?

The below query does not work but it is intended to demonstrate the logic I am tyring to achieve. 下面的查询不起作用,但旨在演示我要实现的逻辑。

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)
}

I've got the query working below. 我的查询在下面工作。 Now the next mystery is why I can't use it to populate a DataGridView in VS. 现在,下一个谜是为什么我不能使用它来填充VS中的DataGridView的原因。 :-( :-(

Working Query: 工作查询:

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 ()
}

Note the: 注意:

hours = g.Sum (x => x.ElapsedHours), mins = g.Sum (x => x.ElapsedMinutes)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM