示例#1
0
        // GET: Songs
        public async Task <IActionResult> Index(string songGenre, string titleString, string artistString,
                                                string albumString)
        {
            // LINQ to obtain a list of genres for a selectList for users
            IQueryable <string> genreQuery = from s in _context.Song
                                             orderby s.Genre
                                             select s.Genre;

            // LINQ to select songs
            var songs = from s in _context.Song
                        select s;

            // query for song title
            if (!string.IsNullOrEmpty(titleString))
            {
                songs = songs.Where(s => s.Title.Contains(titleString));
            }

            // query for song's Genre
            if (!string.IsNullOrEmpty(songGenre))
            {
                songs = songs.Where(s => s.Genre == songGenre);
            }

            // query for artist
            if (!string.IsNullOrEmpty(artistString))
            {
                songs = songs.Where(s => s.Artist.Contains(artistString));
            }

            // query for album
            if (!string.IsNullOrEmpty(albumString))
            {
                songs = songs.Where(s => s.Album.Contains(albumString));
            }

            // create a song genre view model to be rendered based on client choice
            var songGenreViewModel = new SongGenreViewModel();

            // only assign the view model if a query is created, and use distinct to ensure no duplicates
            songGenreViewModel.genres = new SelectList(await genreQuery.Distinct().ToListAsync());
            // also update songs list to reflect query
            songGenreViewModel.songs = await songs.ToListAsync();

            return(View(songGenreViewModel));
        }
示例#2
0
        public SongGenreViewModel GetSongGenre(int id)
        {
            var songGenre = this.DbContext
                            .SongGenres
                            .Include(x => x.Songs)
                            .FirstOrDefault(x => x.Id == id);

            if (songGenre == null)
            {
                return(null);
            }

            var songGenreModel = new SongGenreViewModel()
            {
                GenreName = songGenre.GenreName,
                Songs     = this.Mapper.Map <ICollection <SongConciseViewModel> >(songGenre.Songs)
            };

            return(songGenreModel);
        }