LINQ to Entities无法识别方法'Int32 Last [Int32]

时间:2012-04-01 20:22:52

标签: asp.net linq linq-to-sql

这有什么问题?

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.

1 个答案:

答案 0 :(得分:6)

Linq无法将Last()转换为任何有效的sql语句。所以我的建议是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更改为适合您的内容。