Reputation: 7113
I've a large table of Items and I need to organize them by Category, then by Year and then by Month.
Item has CategoryID and Dated properties.
I got this far:
Dim Items = From Item In DB.Items _
Group By CategoryID = Item.CategoryID _
Into Categories = Group _
Order By CategoryID
But where I put the:
Group By Year = Year(Item.Dated)
and the
Group By Month = Month(Item.Dated)
The final result should be something like this:
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
Thanks
Upvotes: 2
Views: 2822
Reputation: 7113
After reading this (also with more information on this topic):
I come up with:
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
Upvotes: 3