'EF core, Any could not be translated and will be evaluated locally

I have procedure which returns me entity's ids which I need.

(I decide to create this procedure, because entities which should be returned to the end user, are filtered by related entities, but EF Core does not support filtering by related entities).

Then I want to use this ids to get entities I need witch theirs related entities.

I'm using the Any operator. In my opinion it should generate query like this: WHERE id IN(1,2,3,4....)` but it seems it does not work like I want.

Instead, it returns warning with the information that the:

Any clause could not be translated and will be evaluated locally

How I could fix it?

My code below:

var assocsIds = await _context.Assoc.AsNoTracking()
.FromSql($"exec {SqlProcedures.GetAssocsForIndexProc} {query.IndexMviId}, {query.FilterByGroupId}")
.ToListAsync()

var productAssocs = await _context.Assoc
    .Where(x => assocsIds.Any(z => z.Id == x.Id))
    .Include(x => x.Attribute)
    .ThenInclude(x => x.Translation)
    .Include(x => x.Option)
    .ThenInclude(x => x.Translation)
    .Include(x => x.Mvi)
    .ThenInclude(x => x.Translation)
    .AsNoTracking()
    .ToListAsync()
;


Solution 1:[1]

Can you first select "Id"s from assocsIds into another variable and then try the following?

var productAssocs = await _context.Assoc
            .Where(x => yourIdsList.Contains(x.Id))
            .Include(x => x.Attribute)
            .ThenInclude(x => x.Translation)
            .Include(x => x.Option)
            .ThenInclude(x => x.Translation)
            .Include(x => x.Mvi)
            .ThenInclude(x => x.Translation)
            .AsNoTracking()
            .ToListAsync();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jaliya Udagedara