2012-04-01 58 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

相关问题