Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个家庭作业,其中给出IEnumerable<Movie> 了电影包含的位置HashSet<string> genres(由字符串表示的类型列表,如“喜剧”、“戏剧”等)。
IEnumerable<Movie>
HashSet<string> genres
如何使用 LINQ 和 group by 按类型对电影进行排序?(创建IGrouping<string, IEnumerable<Movie>>)
IGrouping<string, IEnumerable<Movie>>
听起来您想先将电影扁平化为动作/流派对,然后分组:
var grouped = from movie in movies from genre in movie.Genres group movie by genre;
或避免查询表达式:
var grouped = movies.SelectMany(movie => move.Genres, (movie, genre) => new { movie, genre }) .GroupBy(pair => pair.genre, pair => pair.movie);