vb.net 对数据表进行分组和求和查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25482492/
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-09-17 18:06:18  来源:igfitidea点击:

Group by and sum query on datatable

vb.netlinq

提问by ruedi

I have a strongly typed DataTablethat looks as follows:

我有一个强类型DataTable,如下所示:

|Team|FirstName|LastName|Hours
|A|Henry|Manny|3
|A|Henry|Manny|5
|A|Johny|Tenny|2
|A|Johny|Tenny|3
|B|Frank|Tank|4
|B|Frank|Tank|5

What I am trying to achieve is to sum the Hours by First and Lastname so my result table should look like:

我想要实现的是按名字和姓氏对小时数求和,因此我的结果表应如下所示:

|Team|FirstName|LastName|Hours
|A|Henry|Manny|8
|A|Johny|Tenny|5
|B|Frank|Tank|9

I tried so many thinks. One of it was:

我尝试了很多想法。其中之一是:

dim query = From row In _dataset.Table1.AsEnumerable
Group by row.FirstName, row.LastName Into MAGroup = Group
Select New with { . First = row.FirstName, .Last = row.LastName, .Sum = sum(MAGroup.Hours)}

But always as soon as I start with the first anonymous member in the Select statement I don't get intellisense help and everything is underlined. I really would be so thankful if anyone could help me with the query.

但是总是只要我从 Select 语句中的第一个匿名成员开始,我就不会得到智能帮助,并且所有内容都带有下划线。如果有人可以帮助我进行查询,我真的会非常感激。

回答by Tim Schmelter

So you want to group by FirstName+LastName? Then group by an anonymous type:

所以你想按FirstName+分组LastName?然后按匿名类型分组:

Dim query = From row In _dataset.Table1.AsEnumerable()
   Group row By Name = New With {
        Key .FirstName = row.FirstName,
        Key .LastName = row.LastName
   } Into NameGroup = Group
   Select New With {
       .First = Name.FirstName,
       .Last = Name.LastName,
       .Sum = NameGroup.Sum(Function(r) r.Hours)
   }