gpt4 book ai didi

vb.net - LINQ - 按年和月分组

转载 作者:行者123 更新时间:2023-12-02 02:17:43 26 4
gpt4 key购买 nike

我正在尝试编写一个 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

最佳答案

您需要按两个属性(年+月)的匿名类型进行分组。您还需要在 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

关于vb.net - LINQ - 按年和月分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12821390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com