I am getting the error :
LINQ to Entities does not recognize the method ‘PagedList.IPagedList1[MvcMusicStore.Models.Album] ToPagedList[Album](System.Collections.Generic.IEnumerable1[MvcMusicStore.Models.Album], Int32, Int32)’ method, and this method cannot be translated into a store expression
For my following controller action :
public ActionResult Browse(string genre, int?page)
{
// Retrieve Genre and its Associated Albums from database
int pageIndex = page ?? 1;
var genreModel = storeDB.Genres.Where(g => g.Name == genre)
.Select(g => new GenreAlbumView
{
ID = g.GenreId,
Name = g.Name,
Albums = g.Albums.ToPagedList(pageIndex, PageSize)
}).SingleOrDefault();
return View(genreModel);
}
What can be the reason and solution to this problem ?
First run your genermodel query:
Then run your select.
Because lin2entity has limited functionality support and can’t convert your function:
to appropriated sql command, in fact it can’t create related expression tree for it, so you should first get entities and then select as a way you want.
If you should do such a paging in DB, create a stored procedure and use it.