【问题标题】:Use of List of ints in EF query - Either rewrite the query in a form that can be translated, or switch to client evaluation在 EF 查询中使用整数列表 - 以可翻译的形式重写查询,或切换到客户端评估
【发布时间】:2020-07-08 15:39:17
【问题描述】:

使用 Entity Framework Core 3.1 我有以下查询:

    var ids = new List<Int32> { 1, 2, 3 }; 

    users = context.Users

      .Where(x => ids.All(y => x.UserSkills.Any(z => y == z.SkillId)));

    var result = await users.ToListAsync();

当我运行此查询时,我收到以下错误:

'The LINQ expression 'DbSet<User>
    .Where(u => True && __ids_0
        .All(y => DbSet<UserSkill>
            .Where(u0 => EF.Property<Nullable<int>>(u, "Id") != null && EF.Property<Nullable<int>>(u, "Id") == EF.Property<Nullable<int>>(u0, "UserId"))
            .Any(u0 => y == u0.SkillId)))' could not be translated. 
    Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync()

我该如何解决这个问题?

【问题讨论】:

    标签: entity-framework-core entity-framework-core-3.1


    【解决方案1】:

    答案是你的错误信息:

    Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync()
    

    所以 linq 命令对于实体框架来说是复杂的。您必须编写一个更简单的 linq 命令,例如:

     var users = context.Users.Where(x => x.UserSkills.Any(z => 1 == z.SkillId)).ToListAsync;
    

    但这不是你想要的。所以你必须为客户端而不是为数据库编写代码语句。

    加载用户,然后在客户端使用您的代码。

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 2021-10-13
      相关资源
      最近更新 更多