我试图从数据表中获得汇总值。但不知道怎么回事。在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
我不想手动循环和组合值。请帮助
您想要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查询和方法语法的混合。