简体   繁体   English

EF无法将表达式转换为sql

[英]EF could not translate expression to sql

It looks like EF is not able to translate the express in the following code, this is the call 看起来EF无法翻译以下代码中的快递,这就是调用

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

this is the method 这是方法

    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();
    }

Any idea what could be the problem? 知道可能是什么问题吗?

It's quite simple, really: Entity Framework just does not support Last(). 实际上,这非常简单:实体框架不支持Last(). The reason for this is that in SQL, you also cannot select the last element (ie you have SELECT TOP but don't have SELECT BOTTOM ). 这是因为在SQL中,您也不能选择最后一个元素(即,您具有SELECT TOP但没有SELECT BOTTOM )。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM