2012-02-02 56 views
0

时对数据库查询进行排序我正在做一个网站,我一直在使用MvcMusicStore作为基地。MVC3 - 使用包括

我想从一个特定类型获取所有专辑,并按艺术家名称排序。我无法真正弄清楚如何按艺术家名字排序。

我的模型:

 

    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
storeDb.Albums.Where(a => a.Genre.Name == genre).OrerBy(a => a.Artist.Name) 
+0

太棒了!你救了我的一天! – 2012-02-02 23:08:44