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.

2 responses to this post.

  1. Do you mind if I quote a few of your posts as long as I provide credit and sources back to your
    blog? My website is in the very same niche as yours and
    my users would genuinely benefit from some of the information you provide here.
    Please let me know if this alright with you. Many thanks!

    Reply

Leave a comment