MVC3 - 使用Include时对db查询进行排序

时间:2012-02-02 22:34:26

标签: asp.net-mvc database linq sorting

我正在建立一个我使用MvcMusicStore作为基础的网站。

我想获取特定类型的所有专辑,并按艺术家名称订购。我无法确定如何按艺术家名称订购。

我的模特:


    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);
    }

如何按艺术家姓名订购结果?

1 个答案:

答案 0 :(得分:4)

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