2009-07-07 5 views
2

J'ai une grande table d'articles et je dois les organiser par catégorie, puis par année, puis par mois.Linq Nested Grouping

L'élément possède des propriétés CategoryID et Dated.

Je suis arrivé jusqu'ici:

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

Mais là où je mets le:

 Group By Year = Year(Item.Dated) 

et

 Group By Month = Month(Item.Dated) 

Le résultat final devrait être quelque chose comme ceci:

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 

Merci

Répondre

3

Après avoir lu ce (aussi avec plus d'informations sur ce sujet):

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

je viens avec:

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