如何使用LINQ或其他方法对表数据进行分组并按多列进行求和

时间:2017-05-09 11:28:04

标签: vb.net linq

我有一个包含类似于以下内容的数据的表格:

Shift--Name----------Department---Time
1      Employee1         360      100
1      Employee1         372      50
1      Employee1         385      300
2      Employee2         301      0
2      Employee2         301      0
2      Employee2         301      0
2      Employee2         305      0
2      Employee2         305      0
2      Employee2         305      0
2      Employee2         305      0
2      Employee2         305      0
2      Employee2         320      0
2      Employee2         333      0
2      Employee2         350      30
2      Employee2         350      35
2      Employee2         350      90
2      Employee2         350      0
2      Employee2         350      0
2      Employee2         350      90
2      Employee2         350      0
2      Employee2         350      0
2      Employee2         350      150
2      Employee2         350      50
2      Employee2         350      0
2      Employee2         350      60
2      Employee3         302      0
2      Employee3         305      0
2      Employee3         305      3
2      Employee3         365      0
2      Employee3         365      0
2      Employee3         365      0
2      Employee3         365      10
2      Employee3         365      1
2      Employee3         365      0
2      Employee3         365      0
2      Employee3         365      10
2      Employee3         365      5
2      Employee4         314      0
2      Employee4         314      0
2      Employee4         314      100

我是VB.NET的新手,但是我需要按shift,然后按名称,然后按部门输出上面的表分组,然后得到每个组的时间总和,如下所示: / p>

Shift--Name----------Department---Time
1      Employee1         360      100
                         ***      100

1      Employee1         372      50
                         ***      50

1      Employee1         385      300
                         ***      300
       ***                        450
***                               450

2      Employee2         301      0
2      Employee2         301      0
2      Employee2         301      0
                         ***      0

2      Employee2         305      0
2      Employee2         305      0
2      Employee2         305      0
2      Employee2         305      0
2      Employee2         305      0
                         ***      0

2      Employee2         320      0
                         ***      0

2      Employee2         333      0
                         ***      0

2      Employee2         350      30
2      Employee2         350      35
2      Employee2         350      90
2      Employee2         350      0
2      Employee2         350      0
2      Employee2         350      90
2      Employee2         350      0
2      Employee2         350      0
2      Employee2         350      150
2      Employee2         350      50
2      Employee2         350      0
2      Employee2         350      60
                         ***      505
       ***                        505

2      Employee3         302      0
                         ***      0

2      Employee3         305      0
2      Employee3         305      3
                         ***      3

2      Employee3         365      0
2      Employee3         365      0
2      Employee3         365      0
2      Employee3         365      10
2      Employee3         365      1
2      Employee3         365      0
2      Employee3         365      0
2      Employee3         365      10
2      Employee3         365      5
                         ***      26
       ***                        29

2      Employee4         314      0
2      Employee4         314      0
2      Employee4         314      100
                         ***      100
       ***                        100

因此,对于employee1,我可以看到他们在360部门总共花了100分钟,在部门372花了50分钟,在部门385花了300分钟。员工1的总时间是450分钟。由于第一班仅有一名员工,第一班的总分钟数为450。 关于如何制定LINQ查询以获得所需分组的任何建议?

如果有比使用LINQ更好的方法,我很乐意听到它,我只是认为LINQ将是解决问题的最通用方式,我可以根据其他类似需求进行修改。

编辑:

我已尝试过以下查询,但未完全理解如何获得我需要的结果:

Dim TimeGroups =
    From j In TotalTimeResults
    Group By x = New With {
        Key .Shift = j.Field(Of String)("Shift"),
        Key .Name = j.Field(Of String)("Name"),
        Key .Time = j.Field(Of Integer)("Time")} Into g = Group
    Select New With {
        .Shift = x.Shift,
        .Name = x.Name,
        .Time = x.Time,
        .total = g.Sum(Function(r) r.Field(Of Integer)("Time"))
    }

我正在使用的表只是一个VB.NET DataTable,其中显示了列和行。

1 个答案:

答案 0 :(得分:0)

我认为这会产生你需要的东西:

Dim TimeGroups = From t In TotalTimeResults
                 Group t By t.Shift Into tsg = Group
                 Select New With { .Shift = Shift, .ShiftTotal = tsg.Sum(Function(t) t.Time),
                     .NameGroup = From t In tsg
                                 Group t By t.Name Into tng = Group
                                 Select New With {
                                     .Name = Name,
                                     .NameTotal = tng.Sum(Function(t) t.Time),
                                     .DeptGroup = From t In tng
                                                 Group t By t.Dept Into tdg = Group
                                                 Select New With { .Dept = Dept, .DeptTotal = tdg.Sum(Function(t) t.Time), .EmpInd = From t In tdg Select t.Time } } }