2013-03-05 22 views
3

我有以下查询在数据库中调用包含通过“直接事件,这些线路的方法时,返回选择通过LINQ最后一个记录失败,

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 
} 

以下错误选择的最后一个记录“不能识别方法” “关于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. 

表结构如下:

int ident IDENTITY NOT NULL, 
int id NOT NULL, 
varchar(50) name NULL, 
//some other varchar fields 
+1

看看这个问题:http://stackoverflow.com/questions/7293639/linq-to-entities-does-not-recognize-the-method-last-really – 2013-03-05 08:56:00

回答

13

拉斯维加斯t不支持Linq to Entities。执行OrderByDescending(通常通过ID或某个日期列)并先选择。喜欢的东西:

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

或者

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

参考请参阅MSDN文章Supported and Unsupported LINQ Methods (LINQ to Entities)