C# Linq 查询按组求和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17299635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:05:29  来源:igfitidea点击:

Linq query to sum by group

c#linqlinq-to-dataset

提问by John

I have a data table like this:

我有一个这样的数据表:

Category         Description       CurrentHours      CTDHours  
LC1              Cat One                 5               0  
LC2              Cat Two                 6               0  
LC3              Cat Three              18               0  
LC1              Cat One                 0               9  
LC2              Cat Two                 0              15  
LC4              Cat Four                0              21  

That I need to Group and Sum to this:

我需要对此进行分组和求和:

Category         Description       CurrentHours      CTDHours  
LC1              Cat One                 5              14  
LC2              Cat Two                 6              21  
LC3              Cat Three              18               0  
LC4              Cat Four                0              21  

In other words I need to sum the two Hours columns grouping by the Category and Description columns.

换句话说,我需要对按类别和说明列分组的两个小时列求和。

I know that I could build a new table and loop through the existing data and sum the data into the new table but I thought there would be an easier way to do it using Linq. I've googled it for a few hours but all the examples I found didn't seem to fit what I was trying to do.

我知道我可以构建一个新表并遍历现有数据并将数据汇总到新表中,但我认为使用 Linq 会更简单。我已经用谷歌搜索了几个小时,但我发现的所有例子似乎都不适合我想要做的。

BTW, the odbc driver that creates the data table does not have the capability for sub queries, etc. or I would have just done it using SQL.

顺便说一句,创建数据表的 odbc 驱动程序没有子查询等功能,否则我会使用 SQL 完成它。

采纳答案by Sergey Berezovskiy

Use anonymous object to group by category and description. Here is Linq to DataSet query which returns grouped hours:

使用匿名对象按类别和描述分组。这是返回分组时间的 Linq to DataSet 查询:

from r in table.AsEnumerable()
group r by new { 
     Category = r.Field<string>("Category"),
     Description = r.Field<string>("Description")
} into g
select new {
   Category = g.Key.Category,
   Description = g.Key.Description,
   CurrentHours = g.Sum(x => x.Field<int>("CurrentHours"),
   CTDHours = g.Sum(x => x.Field<int>("CurrentHours") + x.Field<int>("CTDHours"))
} 

If you are querying database (not clear from question):

如果您正在查询数据库(问题不清楚):

from r in context.Table
group r by new { 
     r.Category,
     r.Description
} into g
select new {
   g.Key.Category,
   g.Key.Description,
   CurrentHours = g.Sum(x => x.CurrentHours),
   CTDHours = g.Sum(x => x.CTDHours + x.CurrentHours)
}

回答by Sam Leach

You need to sum CurrentHoursand CTDhours, so -

你需要总结CurrentHoursCTDhours,所以 -

select new {
   ...
   CTDHours = g.Sum(x => x.Field<int>("CTDHours") + g.Sum(x => x.Field<int>("CurrentHours")
}