Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted
storeDb.Albums.Where(a => a.Genre.Name == genre).OrerBy(a => a.Artist.Name)
share|improve this answer
    
Awesome! You saved my day! –  Mikael Edebro Feb 2 '12 at 23:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.