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

MHF*_*MHF 2 linq asp.net linq-to-sql

这有什么问题?

int folderid = (from p in db.folder where p.isDefault == true select p.id).Last();
Run Code Online (Sandbox Code Playgroud)

我收到这个错误

   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.
Run Code Online (Sandbox Code Playgroud)

Ari*_*ion 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();
Run Code Online (Sandbox Code Playgroud)

我不知道该采取哪种措施,因此您可能需要将其orderby p.id descending改为适合您的东西.