2012-02-02 83 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