我正在尝试从数据表中获取聚合值。但无法弄清楚如何。在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
我需要得到一个结果:
Month, sales, leads, gross
1 5 15 1000
2 3 3 500
我不想手动循环和组合值。请帮忙
答案 0 :(得分:12)
你想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
这是Linq查询和方法语法的混合。