如何用EF逐年分组比按月分组?

时间:2015-01-08 15:19:08

标签: vb.net asp.net-mvc-4 entity-framework-5

我想先按年份分组,然后按月分组归档小部件这样我得到的结果如下:

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

我需要两个选择吗?像第一年一样,然后内部选择?

1 个答案:

答案 0 :(得分:0)

解决了它:

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