3

i have following query to select the last record in the database

using (Entities ent = new Entities())
{
Loaction last = (from x in ent.Locations select x).Last();
Location add = new Location();
add.id = last.id+1;
//some more stuff
}

the following error is returned when calling the method containing these lines via "Direct event" on ext.net:

LINQ to Entities does not recognize the method 'Prototype.DataAccess.Location Last[Location]
(System.Linq.IQueryable`1[Prototype.DataAccess.Location])' method,
and this method cannot be translated into a store expression.

table structure is following:

int ident IDENTITY NOT NULL,
int id NOT NULL,
varchar(50) name NULL,
//some other varchar fields
Vogel612
  • 5,620
  • 5
  • 48
  • 73
  • 1
    Check out this question: http://stackoverflow.com/questions/7293639/linq-to-entities-does-not-recognize-the-method-last-really – default locale Mar 05 '13 at 08:56

1 Answers1

13

Last is not supported by Linq to Entities. Do OrderByDescending (usually by ID or some date column) and select first. Something like:

Location last = (from l in ent.Locations
                 orderby l.ID descending
                 select l).First();

Or

Location last = ent.Locations.OrderByDescending(l => l.ID).First();

See MSDN article Supported and Unsupported LINQ Methods (LINQ to Entities) for reference.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459