Difficult group

I have a homework, where they give us IEnumerable<Movie> where the Movie contains HashSet<string> genres(a list of genres represented by a string such as "Comedy", "Drama", etc.).

How to use LINQ and group to sort movies by genre? (create IGrouping<string, IEnumerable<Movie>>)

+3
source share
1 answer

It looks like you want to align the movies first in the move / genre area, then group:

var grouped = from movie in movies
              from genre in movie.Genres
              group movie by genre;

Or avoid query expressions:

var grouped = movies.SelectMany(movie => move.Genres,
                                (movie, genre) => new { movie, genre })
                    .GroupBy(pair => pair.genre, pair => pair.movie);
+6
source

All Articles