2

我有一个很大的项目表,我需要按类别组织它们,然后按年,然后按月。

项目具有 CategoryID 和 Dated 属性。

我做到了这一点:

Dim Items = From Item In DB.Items _
        Group By CategoryID = Item.CategoryID _
        Into Categories = Group _
        Order By CategoryID 

但我把:

        Group By Year = Year(Item.Dated) 

        Group By Month = Month(Item.Dated)  

最终结果应该是这样的:

For Each Category in Categories
 For Each Year in Category.Years
  For Each Month in Year.Months
   For Each Item in Month.Items

   Next
  Next
 Next
Next

谢谢

4

1 回答 1

3

阅读此内容后(还包含有关此主题的更多信息):

http://msdn.microsoft.com/en-us/vbasic/bb738024.aspx

我想出:

Dim Items = From Item In DB.Items _
  Group Item By CatID = Item.CatID Into Group Select CatID, _
  YearGroups = From y In Group _
    Group y By YearKey = y.PubDate.Value.Year Into YearGroup = Group _
    Select Year = YearKey, _
    MonthGroups = From m In YearGroup _
      Group m By MonthKey = m.PubDate.Value.Month Into MonthGroup = Group _
      Select Month = MonthKey, MonthItems = MonthGroup
于 2009-07-07T22:06:39.857 回答