1

I have the following code which is listing the top 3 'names' in descending order with a count of how many records that 'name' appears in in the table. This is working ok.

Dim top3 = From s In dc.Suggestions
               Group By Name = s.name
               Into t3 = Group, Count()
               Order By Count Descending
               Take 3

I want to amend this so that it will only get a count of the number of records each 'name' appears in for the current month. I can use s.dateRaised to get the date of the record but i'm not entirely sure how to write the code to get only the current month's records?

4

2 回答 2

2

这是对卡尔的回答的轻微修改。我更喜欢这个,因为他的答案不会包含dateRaised在当月最后一天的最后一分钟内有价值的任何东西。

Dim CurrentYear As Integer = DateTime.Today.Year
Dim CurrentMonth As Integer = DateTime.Today.Month

Dim startOfCurrentMonthDate As New DateTime(CurrentYear, CurrentMonth, 1)
Dim startOfNextMonthDate As DateTime = startOfCurrentMonthDate.AddMonths(1)

Dim top3 = From s In dc.Suggestions
           Where s.dateRaised >= startOfCurrentMonthDate AndAlso
                 s.dateRaised < startOfNextMonthDate 
           Group By Name = s.name
           Into t3 = Group, Count()
           Order By Count Descending
           Take 3
于 2013-09-30T16:32:04.113 回答
1

首先,获取当前月份的开始和结束,如下所示:

Dim CurrentYear As Integer = DateTime.Today.Year
Dim CurrentMonth As Integer = DateTime.Today.Month

Dim startOfCurrentMonthDate As New DateTime(CurrentYear, CurrentMonth, 1)
Dim endOfCurrentMonthDate As DateTime = startDate.AddMonths(1).AddMinutes(-1)

现在,您需要在 中使用开始日期和结束日期Where,如下所示:

Dim top3 = From s In dc.Suggestions
           Where s.dateRaised >= startOfCurrentMonthDate And
                 s.dateRaised <= endOfCurrentMonthDate
           Group By Name = s.name
           Into t3 = Group, Count()
           Order By Count Descending
           Take 3
于 2013-09-30T16:01:47.307 回答