如何使用 ID 列表过滤 TableQuery?

How to filter TableQuery with the list of IDs?

我是 Slick 和 Scala 的新手,我正在努力使用 ID 列表过滤查询。

productsNeededIds // = "1,2,3,4,5" - list of Ids of products

问题:如何使用 .filter 从拆分列表中获取包含所有 ID 的查询?

   def productsWithIds(productsNeededIds: String)(implicit ec: ExecutionContext): Future[List[ProductsREST]] = {

    var splitedArray :Array[String] = productsNeededIds.split(",")
    val query = Products.filter(_.prodId === splitedArray)// what should be here instead of ===splitedArray?
}

您应该使用 inSet 方法:

def productsWithIds(productsNeededIds: String)(implicit ec: ExecutionContext): Future[List[ProductsREST]] = {

  val splitedSet: Set[String] = productsNeededIds.split(",").toSet
  val query = Products.filter(_.prodId.inSet(splitedSet))
}

这是假设您的产品 ID 是字符串。如果它们是 Int,您当然应该先将 splittedSet 映射到 Int