简体   繁体   English

无法翻译 LINQ 表达式错误

[英]LINQ expression error could not be translated

I'm getting this error with trying to execute my query.我在尝试执行查询时遇到此错误。

System.InvalidOperationException: .Where(ti => (int)ti.Inner.OptionType == 1 && ti.Inner.QualifierId == null && ti.Inner.CreditingMethod != "xxx" && __ToList_0 .Any(e => e.Vdate== (Nullable)ti.Outer.ADate))' could not be translated. System.InvalidOperationException: .Where(ti => (int)ti.Inner.OptionType == 1 && ti.Inner.QualifierId == null && ti.Inner.CreditingMethod != "xxx" && __ToList_0 .Any(e => e .Vdate== (Nullable)ti.Outer.ADate))' 无法翻译。 Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。 See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038

The object that I'm populating is我填充的对象是

public class OCList
    {
        public DateTime? Vdate{ get; set; }
        public Double? VWeight{ get; set; }
    }

/// example of adding to the list
List<OCList> ocList= new List<OCList>();
            if (request.Date1 != null && request.Weight1 != null)
            {
                ocList.Add(new OCList{ Vdate = request.Date1.Value, VWeight = request.Weight1.Value });
            }

it error's here:错误在这里:

&& ocList.ToList().Any(e => e.Vdate == x.Tb2.ADate))

Linq expression:林克表达式:

var results = _context.Tb1
                .Join(_context.Tb2, oc => oc.OptionId, o => o.OptionId, (oc, o) => new { OptionCost = oc, Option = o })
                .Where(x => x.Tb2.Val1 == 1 
                    && x.Tb2.Val2 == null 
                    && ocList.ToList().Any(e => e.Vdate == x.Tb2.ADate))
                ////.........

Check client and server side evaluation here: https://docs.microsoft.com/en-us/ef/core/querying/client-eval在此处检查客户端和服务器端评估: https : //docs.microsoft.com/en-us/ef/core/querying/client-eval

EF Core supports partial client evaluation in the top-level projection (essentially, the last call to Select()). EF Core 支持顶级投影中的部分客户端评估(本质上是对 Select() 的最后一次调用)。 If the top-level projection in the query can't be translated to the server, EF Core will fetch any required data from the server and evaluate remaining parts of the query on the client.如果查询中的顶级投影无法转换到服务器,EF Core 将从服务器获取任何所需数据并在客户端评估查询的其余部分。 If EF Core detects an expression, in any place other than the top-level projection, which can't be translated to the server, then it throws a runtime exception.如果 EF Core 在顶级投影以外的任何位置检测到无法转换到服务器的表达式,则会引发运行时异常。

Your ToList and then Any can't be translated to sql and thus you get the error.您的ToListAny无法转换为 sql,因此您会收到错误消息。

This would work这会工作

var results = _context.Tb1
                .Join(_context.Tb2, oc => oc.OptionId, o => o.OptionId, (oc, o) => new { OptionCost = oc, Option = o })
                .AsEnumerable()
                .Where(x => x.Tb2.Val1 == 1 
                    && x.Tb2.Val2 == null 
                    && ocList.ToList().Any(e => e.Vdate == x.Tb2.ADate))

BUT it would first fetch everything from the server and then apply the where clause, resulting to poor performance.但它会首先从服务器获取所有内容,然后应用where子句,从而导致性能不佳。

You could make it a bit better like this你可以像这样让它更好一点

var results = _context.Tb1
                .Join(_context.Tb2, oc => oc.OptionId, o => o.OptionId, (oc, o) => new { OptionCost = oc, Option = o })
                .Where(x => x.Tb2.Val1 == 1 
                    && x.Tb2.Val2 == null)
                .AsEnumerable()
                 .Where(ocList.ToList().Any(e => e.Vdate == x.Tb2.ADate))

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

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