views:

338

answers:

2

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

A: 

I think this question covers this topic: http://stackoverflow.com/questions/442425/nested-group-by-in-linq

Alex Black
the problem was constructing the syntax, and that topic was in C#, I needed the VB format, but thanks anyway
DK39
+1  A: 

After reading this (also with more information on this topic):

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

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
DK39