Пример #1
0
        private void UpdateSongGenres(string[] selectedGenres, Song songToUpdate)
        {
            if (selectedGenres == null)
            {
                songToUpdate.SongGenres = new List <SongGenre>();
                return;
            }

            var selectedGenresHS = new HashSet <string>(selectedGenres);
            var songGenres       = new HashSet <int>
                                       (songToUpdate.SongGenres.Select(c => c.Genre.GenreID));

            foreach (var genre in repository.Genres())
            {
                if (selectedGenresHS.Contains(genre.GenreID.ToString()))
                {
                    if (!songGenres.Contains(genre.GenreID))
                    {
                        songToUpdate.SongGenres.Add(new SongGenre {
                            SongID = songToUpdate.SongID, GenreID = genre.GenreID
                        });
                    }
                }
                else
                {
                    if (songGenres.Contains(genre.GenreID))
                    {
                        SongGenre SongGenreToRemove = songToUpdate.SongGenres.FirstOrDefault(i => i.GenreID == genre.GenreID);
                        repository.DeleteSongGenre(SongGenreToRemove);
                    }
                }
            }
        }