2013-03-05 49 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)