Efcore: Avoiding client side evaluation

Created on 29 Nov 2019  路  2Comments  路  Source: dotnet/efcore

I have a piece of code which contains nested ANY. When it gets to that part it throws classic exception about converting to client side evaluation. I would like to know if there is a way to rewrite the code and avoid converting this query to client side evaluation. Here is the code:

var query = _assignmentRepository
                .GetAll()
                .Include(x => x.AssignmentUsersAssigned).ThenInclude(y => y.User)
                .WhereIf(input.UserIds.Any(), x => input.UserIds.Contains(x.CreatorUserId.Value) || input.UserIds.Any(y => x.AssignmentUsersAssigned.Any(z => z.UserId == y)));

Input.UserIds is of type List long. Any ideas?

closed-question customer-reported

Most helpful comment

Rewrite this part:
input.UserIds.Any(y => x.AssignmentUsersAssigned.Any(z => z.UserId == y))
in this way:
x.AssignmentUsersAssigned.Any(z => input.UserIds.Contains(z.UserId))

All 2 comments

Rewrite this part:
input.UserIds.Any(y => x.AssignmentUsersAssigned.Any(z => z.UserId == y))
in this way:
x.AssignmentUsersAssigned.Any(z => input.UserIds.Contains(z.UserId))

Rewrite this part:
input.UserIds.Any(y => x.AssignmentUsersAssigned.Any(z => z.UserId == y))
in this way:
x.AssignmentUsersAssigned.Any(z => input.UserIds.Contains(z.UserId))

That does it, thanks!

Was this page helpful?
0 / 5 - 0 ratings