Search code examples
c#linqigrouping

Nested IGrouping with LinQ


I want to know if it's possible to get a List<IGrouping<string, IGrouping<string, T>>> out of a Linq expression. What I want to do is order songs by genre and artist, but in a format where I can iterate through each genre and for every genre, iterate for each album. If it's possible how can it be done? Or is there a better approach to what I want to achieve?

Here's my Song class

public class Song
{
    [StringLength(100)]
    public string Album { get; set; }

    public int Id { get; set; }

    [StringLength(100)]
    public string Title { get; set; }

    [StringLength(100)]
    public string Genre { get; set; }

    [StringLength(255)]
    public string Comment { get; set; }

    [StringLength(4)]
    public string Year { get; set; }

    [StringLength(100)]
    public string Author { get; set; }

    public int Length { get; set; }

    [StringLength(255)]
    public string Photo { get; set; }

    [StringLength(255)]
    public string FileName { get; set; }

    public string Extension { get; set; }
}

And this query I had here

var songs = db.Songs.GroupBy(s => s.Genre).ToList();

gives me a List<IGrouping<string, Song>> but what I want is to further group by Artist.


Solution

  • What I wanted was a List<IGrouping<string, IGrouping<string, Song>>> and this code gives me exactly what I want

                var queryNestedGroups =
                (from song in db.Songs
                group song by song.Genre into newGroup1
                from newGroup2 in
                    (from song in newGroup1
                     group song by song.Author)
                group newGroup2 by newGroup1.Key).ToList();
    

    (though I'm not quite sure how to translate it into linq method syntax).

    Thanks to everyone who commented and/or answered.