简体   繁体   中英

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 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()
            ;

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM