[英]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.我尝试了使用
Any
和Contains
两种方法,但都不起作用。 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.