0

I want to filter a dbset using a where clause but both predicates dont work when using AsQueryable and the exception:

The linq expression could not be translated

is thrown

...
var objects = myObject; //Ienumerable

return await dbset.AsQueryable()
.Where(entity => objects.Any(obj => obj.uId == entity.uId &&  obj.cId == entity.cId))
.ToArrayAsync(cancellationToken)
.ConfigureAwait(false); // not working

return await dbset.AsQueryable().
Where(entity => objects.Contains(new Object(entity.cId, entity.uId))
.ToArrayAsync(cancellationToken)
.ConfigureAwait(false); // also not working

When I change AsQueryable to AsEnumerable it is working but since the set can be quite large I want to stick with AsQueryable. How could I rewrite the where clause so that the exception is not thrown?

n1nni
  • 76
  • 1
  • 8
G.Don
  • 17
  • 6

1 Answers1

0

EF Core query provider is unable to translate certain operations from your LINQ expression to SQL when using AsQueryable. The methods Any and Contains in your current queries are causing this issue. You must rewrite your query to make it more compatible EF Core's query translation capabilities.

var objectIds = objects.Select(object => new { object.uId, object.cId }).ToList();

return await dbset.AsQueryable()
    .Join(objectIds,
        entity => new { entity.uId, entity.cId },
        objectId => objectId,
        (entity, objectId) => entity)
    .ToArrayAsync(cancellationToken)
    .ConfigureAwait(false);
n1nni
  • 76
  • 1
  • 8