Posts Tagged ‘linq’

LINQ to Entities does not recognize the method Last()

Solve the error LINQ to Entities does not recognize the method Last().

Hi all, in this post I will show you how to resolve a little problem when using ADO.net entity framework

Details of the problem: when trying to use the method last() to have the last record in the database table using thi code :

databaseEntities db = new databaseEntities();
UserSet user = new UserSet();
user = (from e1 in db.UserSet select e1).Last();

it throws an exception that says :

LINQ to Entities does not recognize the method ‘Myapp.UserSet Last[UserSet](System.Linq.IQueryable`1[Myapp.UserSet])’ method, and this method cannot be translated into a store expression.

Solution:

replace the instruction : user = (from e1 in db.UserSet select e1).Last();  with user = (from e1 in db.UserSet select e1).ToList().Last();  and it will work properly

Hope that was helpful.