1
votes

I'm doing a site where i've been using the MvcMusicStore as a base.

I want to get all the albums from a specific genre, and order these by Artist name. I can't really figure out how to order by Artist name.

My models:


    public partial class Genre
    {
        public int GenreId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public ICollection Albums { get; set; }
        public ICollection Artists { get; set; }
    }

    public class Artist
    {
        public int ArtistId { get; set; }
        public string Name { get; set; }
    }

    public class Album
    {
        public int AlbumId { get; set; }
        public int GenreId { get; set; }
        public int ArtistId { get; set; }
        public string Title { get; set; }
        public string Price { get; set; }
        public string AlbumArtUrl { get; set; }
        public string Description { get; set; }

        public virtual Genre Genre { get; set; }
        public virtual Artist Artist { get; set; }
    }

    My Controller:

    public ActionResult Index(string genre = "CD/LP")
    {
        var genreModel = storeDb.Genres.Include("Albums").Include("Artists")
                .Where(g => g.Name == genre).FirstOrDefault();

        return View(genreModel);
    }

How can I order the results by Artist name?

1

1 Answers

4
votes
storeDb.Albums.Where(a => a.Genre.Name == genre).OrerBy(a => a.Artist.Name)