在EF查询中使用整数列表-以可以翻译的形式重写查询,或切换到客户评估

时间:2020-03-27 20:38:37

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

使用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()

我该如何解决?

1 个答案:

答案 0 :(得分:0)

答案是您的错误消息:

import scala.io.Source._
import org.apache.spark.sql.{Dataset, SparkSession}

var res = fromURL(url).mkString.stripMargin.lines.toList
val csvData: Dataset[String] = spark.sparkContext.parallelize(res).toDS()

val frame = spark.read.option("header", true).option("inferSchema",true).csv(csvData)
frame.printSchema()

因此linq命令会使实体框架复杂化。您必须编写一个更简单的linq命令,例如:

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

但这不是您想要的。因此,您必须为客户端而不是数据库编写代码语句。

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