private void PopulateAssignedGenres(Song song) { var allGenres = repository.Genres(); var SongGenres = new HashSet <int>(song.SongGenres.Select(g => g.GenreID)); var viewModel = new List <AssignedGenre>(); foreach (var genre in allGenres) { viewModel.Add(new AssignedGenre { GenreID = genre.GenreID, Name = genre.Name, Assigned = SongGenres.Contains(genre.GenreID) }); } ViewData["Genres"] = viewModel; }