简体   繁体   English

System.InvalidOperationException:无法使用带有 PostgreSQL 的 EF Core 翻译 LINQ 表达式

[英]System.InvalidOperationException: The LINQ expression could not be translated using EF Core with PostgreSQL

I want to check if a list contains any item from another list using EF Core with Npsql provider.我想检查一个列表是否包含使用 EF Core 和 Npsql 提供程序的另一个列表中的任何项目。 Then I want to get the exact item that was matched in my Dto.然后我想获得在我的 Dto 中匹配的确切项目。

My code is the following (note: Reports.Models is List<string> and so is request.Models as well. The request is consumer filter/search):我的代码如下(注意: Reports.Models 是List<string> ,request.Models 也是如此。请求是消费者过滤/搜索):

var x = await _dbContext.Reports
            .Where(x => x.Models.Any(i => request.Models.Contains(i)))
            .Select(x => new ReportDto
            {
                // Model = x.Identifiers.First(i => request.Identifiers.Contains(i)) // this also fails.
                Model = request.Models.First(i => request.Models.Any(y => y == i)), // fails on this line
            })
            .ToListAsync(cancellationToken);

I tried both ways using Any and Contains , neither work.我尝试了使用AnyContains两种方法,但都不起作用。 They both return the same error which says:他们都返回相同的错误,上面写着:

System.InvalidOperationException: The LINQ expression 'i => __request_Models_0.Contains(i)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

But I don't understand why?但我不明白为什么? The error never changes, even in my case when I use Any , it still complains about Contains .错误永远不会改变,即使在我使用Any的情况下,它仍然会抱怨Contains

You are effectively trying to push your request.Models to the database Server for it to evaluate if any of its datasets are in it.您正在有效地尝试将您的request.Models推送到数据库服务器,以评估其是否有任何数据集。 That won't work.那是行不通的。

You need to request the Models from the database first and compare them locally OR transform your request.Models into a set of IDs that the database can compare against.您需要首先从数据库中请求模型并在本地进行比较,或者将您的request.Models转换为数据库可以比较的一组 ID。

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

相关问题 System.InvalidOperationException:LINQ 表达式 - System.InvalidOperationException: The LINQ expression EF Core:使用显式转换时“无法翻译 LINQ 表达式” - EF Core: "The LINQ expression could not be translated" when using explicit casting System.InvalidOperationException:LINQ 表达式....'无法翻译。 ASP.NET 6...方法“DecodeFrom64”的翻译失败 - System.InvalidOperationException: The LINQ expression.... 'could not be translated. ASP.NET 6 ...Translation of method 'DecodeFrom64' failed 无法翻译 LINQ 表达式 - EF Core - The LINQ expression could not be translated - EF Core 无法在 EF Core 3.1 中翻译 LINQ 表达式 - The LINQ expression could not be translated in EF Core 3.1 无法翻译 EF Core Linq 表达式 - EF Core Linq expression could not be translated EF Core (LINQ) - 无法翻译查询表达式 - EF Core (LINQ) - The Query expression could not be Translated System.InvalidOperationException: LINQ 表达式 'GroupByShaperExpression: - System.InvalidOperationException: The LINQ expression 'GroupByShaperExpression: EF Core 3 Linq 无法翻译 - EF Core 3 Linq could not be translated EF Core 3.1 - 无法翻译 LINQ 表达式(左连接与 group by) - EF Core 3.1 - The LINQ expression could not be translated (left joins with group by)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM