linq选择最后一条记录失败,"方法无法识别"

Vog*_*612 3 linq linq-to-entities

我有以下查询来选择数据库中的最后一条记录

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
}
Run Code Online (Sandbox Code Playgroud)

在ext.net上通过"Direct event"调用包含这些行的方法时返回以下错误:

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.
Run Code Online (Sandbox Code Playgroud)

表结构如下:

int ident IDENTITY NOT NULL,
int id NOT NULL,
varchar(50) name NULL,
//some other varchar fields
Run Code Online (Sandbox Code Playgroud)

Ser*_*kiy 13

Linq to Entities不支持Last.做OrderByDescending(通常通过ID或某个日期列)并首先选择.就像是:

Location last = (from l in ent.Locations
                 orderby l.ID descending
                 select l).First();
Run Code Online (Sandbox Code Playgroud)

要么

Location last = ent.Locations.OrderByDescending(l => l.ID).First();
Run Code Online (Sandbox Code Playgroud)

有关参考,请参阅MSDN文章支持和不支持的LINQ方法(LINQ to Entities).