EF核心,Any无法翻译,将在本地进行评估

时间:2018-02-12 08:28:46

标签: c# entity-framework asp.net-core entity-framework-core

我有程序可以返回我需要的实体ID。

(我决定创建这个程序,因为应该向最终用户回复的实体被相关实体过滤,但是ef核心不支持相关实体的过滤。)

然后我想用这个id来获取我需要的实体,这些实体是他们的相关实体。

我正在使用"任何"运营商。在我看来,它应该产生这样的查询:" WHERE id IN(1,2,3,4 ....)"但看起来,它并不像我想的那样工作。

相反,它返回警告,其中包含无法翻译" Any子句的信息,并将在本地评估"

我该如何解决?

我的代码如下:

            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 :(得分:0)

您可以先从 assocsIds 中选择“Id”到另一个变量中,然后尝试以下操作吗?

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