我有这样的功能:
def getList(gig: Gig, createdBy: User = null, all: Boolean = true): List[Bid] = {
var bys = List[QueryParam](By(Bid.gig, gig))
if (createdBy!=null) bys = By(Bid.createdBy, createdBy) :: bys
if (!all) bys = By(Bid.deleted, false) :: bys
Bid.findAll(bys) //gives error as do not accept List[QueryParam]
}
如何向findAll提供动态数量的QueryParams?
答案 0 :(得分:0)
像这样:
Bid.findAll(bys:_*)
旁注:值得避免使用null
或Option
。
def getList(gig: Gig, createdBy: Option[User] = None, all: Boolean = true): List[Bid] = {
然后你可以做
val bys = (if (!all) Some(By(Bid.deleted, false)) else None) ++
(createdBy.map(u => By(Bid.createdBy, u))) ++
List[QueryParam](By(Bid.gig, gig))
Bid.findAll(bys:_*)