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

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.

Linq无法将Last()转换为任何有效的sql语句.所以我的建议是orderby decendingTake(1)

Linq can not translate the Last() to any valid sql statment. So my suggestion would be to orderby decending and Take(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更改为适合您的内容.

I don't know which to take so you might have to change the orderby p.id descending to something that suites you.