如何使用 EF 按年而不是按月分组?
How to group first by year than by month with EF?
我想先按年对帖子进行分组,然后按月对存档小部件进行分组,这样我得到的结果如下:
2012
January
Febuary
March
Etc
2013
January
Febuary
March
Etc
这是我目前拥有的:
Public Function SelectYearandMonth() As IEnumerable(Of ArchiveMonthYearViewModel)
Dim posts
posts = _postRepository.SelectAll.Where(Function(p)
p.PostIsPublished).GroupBy(Function(p) New ArchiveMonthYearViewModel()
With {.Year = p.PostDateCreated.Year, .Month =
.PostDateCreated.Month}).Select(Function(a) a.Key).ToList
End Sub
我需要两次选择吗?首先是年份,然后是内部选择?
已解决:
Public Function SelectYearandMonth() As IEnumerable(Of ArchiveMonthYearViewModel)
Dim data = db.be_Posts.Where(Function(p) p.IsPublished = True).GroupBy(Function(a) _
New With {.Month = a.DateCreated.Value.Month, .Year = a.DateCreated.Value.Year}).Select(Function(x) _
New ArchiveMonthYearViewModel With {.Month = x.Key.Month, .Year = x.Key.Year,
.Total = x.Count}).OrderByDescending(Function(x) x.Year).ThenByDescending(Function(x)
(x.Month)).ToList
Return data
End Function
我想先按年对帖子进行分组,然后按月对存档小部件进行分组,这样我得到的结果如下:
2012
January
Febuary
March
Etc
2013
January
Febuary
March
Etc
这是我目前拥有的:
Public Function SelectYearandMonth() As IEnumerable(Of ArchiveMonthYearViewModel)
Dim posts
posts = _postRepository.SelectAll.Where(Function(p)
p.PostIsPublished).GroupBy(Function(p) New ArchiveMonthYearViewModel()
With {.Year = p.PostDateCreated.Year, .Month =
.PostDateCreated.Month}).Select(Function(a) a.Key).ToList
End Sub
我需要两次选择吗?首先是年份,然后是内部选择?
已解决:
Public Function SelectYearandMonth() As IEnumerable(Of ArchiveMonthYearViewModel)
Dim data = db.be_Posts.Where(Function(p) p.IsPublished = True).GroupBy(Function(a) _
New With {.Month = a.DateCreated.Value.Month, .Year = a.DateCreated.Value.Year}).Select(Function(x) _
New ArchiveMonthYearViewModel With {.Month = x.Key.Month, .Year = x.Key.Year,
.Total = x.Count}).OrderByDescending(Function(x) x.Year).ThenByDescending(Function(x)
(x.Month)).ToList
Return data
End Function