[英]System.InvalidOperationException: The LINQ expression could not be translated using EF Core with PostgreSQL
我想檢查一個列表是否包含使用 EF Core 和 Npsql 提供程序的另一個列表中的任何項目。 然后我想獲得在我的 Dto 中匹配的確切項目。
我的代碼如下(注意: 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);
我嘗試了使用Any
和Contains
兩種方法,但都不起作用。 他們都返回相同的錯誤,上面寫着:
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.
但我不明白為什么? 錯誤永遠不會改變,即使在我使用Any
的情況下,它仍然會抱怨Contains
。
您正在有效地嘗試將您的request.Models
推送到數據庫服務器,以評估其是否有任何數據集。 那是行不通的。
您需要首先從數據庫中請求模型並在本地進行比較,或者將您的request.Models
轉換為數據庫可以比較的一組 ID。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.