如何使用Scala Slick 2.0在.where()子句中生成OR查询?

tux*_*dna 2 scala playframework slick playframework-2.2

我想在whereSlick查询的方法中添加两个子句.我有代码:

val users = TableQuery[Users]
val actions = TableQuery[Acts]
val filteredUsers = users.where(.....) // Some condition
val acts = actions.where(_.domain === 10)
val ownerAct = acts.where(_.owner in filteredUsers)
val assigneeAct = acts.where(_.assignee in filteredUsers)
Run Code Online (Sandbox Code Playgroud)

我想获取属于过滤用户assigneeownerer过滤用户的操作.我希望代码是这样的:

val users = TableQuery[Users]
val actions = TableQuery[Acts]
val filteredUsers = users.where(.....) // Some condition
val acts = actions.where(_.domain === 10)

val ownerOrAssigneeAct = acts.where(_.owner in filteredUsers).
                                 or(_.assignee in filteredUsers)
Run Code Online (Sandbox Code Playgroud)

如何使用Scala Slick 2.0在.where()中生成这样的OR查询?

End*_*Neu 9

有几件事,首先要使用in你的查询必须返回一些东西,而不是

val filteredUsers = users.where(.....)
Run Code Online (Sandbox Code Playgroud)

应该(取自这个答案):

val filteredUsers = users.filter(/* condition */).map(_.id)
Run Code Online (Sandbox Code Playgroud)

假设你想匹配id,那么你可以简单地使用匿名函数组成条件:

val ownerOrAssigneeAct = 
  acts.filter(act => act.domain === 10 && (act.owner in filteredUsers || act.assignee in filteredUsers))
Run Code Online (Sandbox Code Playgroud)

请注意,我没有使用,where因为它已被弃用,请filter改用.

  • acts.filter(act => act.domain === 10).filter(filterUsers中的act.owner | filterUsers中的act.assignee))同样没问题. (2认同)