2017-06-17 61 views
0

我對Slick和Scala非常陌生,我正努力用ID列表過濾查詢。如何使用ID列表篩選TableQuery?

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? 
} 

回答

1

您應該使用inSet方法:

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

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

這是假設你的產品ID都是字符串。如果它們是Int,那麼當然應該先將splittedSet映射到Int