对数据表进行分组依据和求和

时间:2014-08-25 09:11:20

标签: vb.net linq

我有一个强类型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

我想要实现的是将小时数除以名字和姓氏,这样我的结果表应如下所示:

|Team|FirstName|LastName|Hours
|A|Henry|Manny|8
|A|Johny|Tenny|5
|B|Frank|Tank|9
我试过这么多想法。其中一个是:

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

但是,一旦我从Select语句中的第一个匿名成员开始,我就不会得到intellisense帮助,并且所有内容都有下划线。如果有人能帮我查询,我真的很感激。

1 个答案:

答案 0 :(得分:4)

您希望按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)
   }