数据表组由vq.net中的linq组成

Nic*_*ick 9 linq vb.net datatable group-by

我正在尝试从数据表中获取聚合值.但无法弄清楚如何.在c#中看到一些例子,但是无法将它们翻译成vb.net.

我有数据表

Month, campaign, sales, leads, gross
1           1                5         10       1000
1            2               0          5         0
2            1               2          0         300
2            2               1          3         200
Run Code Online (Sandbox Code Playgroud)

我需要得到一个结果:

Month, sales, leads, gross
1           5        15       1000
2           3         3         500
Run Code Online (Sandbox Code Playgroud)

我不想手动循环和组合值.请帮忙

Tim*_*ter 13

你想要Group by Month吗?您可以使用Sum以对组进行求和:

Dim query = From row In dt
        Group row By Month = row.Field(Of Int32)("Month") Into MonthGroup = Group
        Select New With {
            Key Month,
            .Sales = MonthGroup.Sum(Function(r) r.Field(Of Int32)("Sales")),
            .Leads = MonthGroup.Sum(Function(r) r.Field(Of Int32)("Leads")),
            .Gross = MonthGroup.Sum(Function(r) r.Field(Of Int32)("Gross"))
       }

For Each x In query
    Console.WriteLine("Month:{0} {1} {2} {3}", x.Month, x.Sales, x.Leads, x.Gross)
Next
Run Code Online (Sandbox Code Playgroud)

这是Linq查询和方法语法的混合.