I have a table that have several field and each of them update separately by separate ViewModel , Now I wanna to get the latest Value of a specific field (maybe it has updated in fifth record of my table) , OK? now what I have write is look like this :
public ViewResult ShowPiece()
{
var context = new SiteContext();
var showPiece = context.Portraits.LastOrDefault();
return View(showPiece);
}
but when I run the application and navigate above action , I got thie Error :
LINQ to Entities does not recognize the method , and this method cannot be translated into a store expression...
what is the problem with that ??
Try something like this:
I had that situation and this worked for me.
I've tried every of the replies that you've here.
But no one had really worked.
My solution was:
Use descending ordering (by date, or id) and
FirstOrDefault
which is supported:Another option, is select portrait which has max date (id) via subquery (as Evelie suggested in comments):
I made a little investigation here. In first case (ordering) following query is generated:
In second case (getting max):
Execution plan is almost same, but in second case Top is executed twice. Thus Top costs 0% comparing to Index Scan, this should not be a problem.