I think that from the point of view of simplicity this should be the same result, but faster, since it does not require two rounds through EF to the sql server, you always want to execute the query as little time as possible for the delay, since the Id field is the primary key and is indexed must be performed
using(var db = new DataContext()) { var maxEntity = db.Entities.OrderByDecending(x=>x.Id).FirstOrDefault() }
There should be an equivalent sql query
SELECT TOP 1 * FROM Entities Order By id desc
to enable search query
string predicate = "name"; using(var db = new DataContext()) { var maxEntity = db.Entities .Where(x=>x.Name == predicate) .OrderByDecending(x=>x.Id) .FirstOrDefault() }
source share