EF无法将表达式转换为sql

时间:2017-10-03 09:32:15

标签: sql entity-framework expression

看起来EF无法在以下代码中翻译快递,这是调用

Counter lastCounter = unitOfWork.CounterRepository.FindLast(x => x.Div == counter.Div, x => x.Div);

这是方法

    public Counter FindLast(Expression<Func<Counter, bool>> predicate, params Expression<Func<Counter, object>>[] includedProperties)
    {
        IQueryable<Counter> set = context.Set<Counter>().Where(predicate);

        foreach (var includeProperty in includedProperties)
        {
            set = set.Include(includeProperty);
        }
        return set.Last();
    }

知道可能是什么问题吗?

1 个答案:

答案 0 :(得分:0)

这非常简单,实际上:实体框架不支持Last().原因是在SQL中,你也无法选择最后一个元素(即你有SELECT TOP但是没有SELECT BOTTOM)。

请参阅https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ef/language-reference/supported-and-unsupported-linq-methods-linq-to-entities