I want to check if a list contains any item from another list using EF Core with Npsql provider. Then I want to get the exact item that was matched in my 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):
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. 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.