Adding a LINQ query to get all albums to improve performance

This commit is contained in:
Praburaj 2014-08-29 13:41:43 -07:00
parent 45406bf082
commit 01c6489454
1 changed files with 23 additions and 7 deletions

View File

@ -23,14 +23,30 @@ namespace MusicStore.Areas.Admin.Controllers
public IActionResult Index() public IActionResult Index()
{ {
// TODO [EF] Swap to native support for loading related data when available // TODO [EF] Swap to native support for loading related data when available
var albums = db.Albums; var albums = from album in db.Albums
foreach (var album in albums) join genre in db.Genres on album.GenreId equals genre.GenreId
{ join artist in db.Artists on album.ArtistId equals artist.ArtistId
album.Genre = db.Genres.Single(g => g.GenreId == album.GenreId); select new Album()
album.Artist = db.Artists.Single(a => a.ArtistId == album.ArtistId); {
} ArtistId = album.ArtistId,
AlbumArtUrl = album.AlbumArtUrl,
AlbumId = album.AlbumId,
GenreId = album.GenreId,
Price = album.Price,
Title = album.Title,
Artist = new Artist()
{
ArtistId = album.ArtistId,
Name = artist.Name
},
Genre = new Genre()
{
GenreId = album.GenreId,
Name = genre.Name
}
};
return View(albums.ToList()); return View(albums);
} }
// //