我正在尝试编写一个linq查询来按年/月分组来自数据表的数据。
我成功地分组了一个或另一个但不是两个......
Dim q = From p As DataRow In DTCalls.Rows _
Group By Year = p("DATE").Year _
Into CALLS = Count(p("CALL_ID")) _
Select Year, CALLS
如何在同一个查询中按年份对年份进行分组?
来源数据:
DATE CALL_ID
2012-05-01 23:52:44 6587
2012-02-03 09:17:41 6562
2012-05-01 06:32:41 6565
2012-02-03 12:47:41 6565
2011-05-31 08:37:41 6511
预期产出:
DATE COUNT
2011-05 1
2012-02 2
2012-05 2
答案 0 :(得分:3)
您需要按匿名类型分为两个属性(年+月)。您还需要在VB.NET中应用Key
关键字。
Anonymous Types (Visual Basic)
Dim dateGroups =
From row In table
Let year = row.Field(Of Date)("DATE").Year
Let month = row.Field(Of Date)("DATE").Month
Group row By YearMonth = New With {
Key .year = year,
Key .month = month
} Into DateGroup = Group
Order By YearMonth.year, YearMonth.month
For Each grp In dateGroups
Console.WriteLine("Date: {0}-{1} Count:{2}",
grp.YearMonth.year,
grp.YearMonth.month,
grp.DateGroup.Count())
Next
答案 1 :(得分:0)
您需要创建包含这两个值的匿名类型,或者您可以使用元组:
Group By Tuple.Create(p("DATE").Year, p("DATE").Month)