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

asp.net-core c# entity-framework entity-framework-core

Question

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

(I decide to create this procedure, because entities which should be returend 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 "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?

Me 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()
            ;
1
0
2/12/2018 8:28:45 AM

Accepted Answer

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();
0
2/12/2018 9:04:19 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow