2012-04-01 59 views
2

這有什麼問題?LINQ to Entities不識別方法'Int32 Last [Int32]

int folderid = (from p in db.folder where p.isDefault == true select p.id).Last(); 

我得到這個錯誤

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

回答

5

的Linq不能Last()翻譯成任何有效的SQL statment。所以我的建議是orderby decendingTake(1)

也許是這樣的:

int? folderid =(
     from p in db.folder 
     where p.isDefault == true 
     orderby p.id descending 
     select p.id 
    ).Take(1).SingleOrDefault(); 

我不知道哪個拿,所以你可能需要的orderby p.id descending改變的東西,套房,爲您。

+0

'FirstOrDefault()'如果集合爲空則不會炸掉它。但仍然是我+1,這是正確的方法。 – 2012-04-01 20:40:58

+0

你不覺得'Single()'或'SingleOrDefault()'會更直觀嗎? – MarcinJuraszek 2012-04-01 20:42:22

+0

如果集合爲空,異常將拋出。 是真的嗎? – MHF 2012-04-01 20:45:27

相關問題