Łuk*_*rka 4 scala playframework slick
我真的是 Slick 和 Scala 的新手,我正在努力使用 id 列表过滤查询。
productsNeededIds // = "1,2,3,4,5" - list of Ids of products
Run Code Online (Sandbox Code Playgroud)
问题:如何使用 .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?
}
Run Code Online (Sandbox Code Playgroud)
You should use the inSet method:
def productsWithIds(productsNeededIds: String)(implicit ec: ExecutionContext): Future[List[ProductsREST]] = {
val splitedSet: Set[String] = productsNeededIds.split(",").toSet
val query = Products.filter(_.prodId.inSet(splitedSet))
}
Run Code Online (Sandbox Code Playgroud)
That's assuming your product IDs are strings. If they're Ints instead, you should map your splittedSet to Ints first, of course.