如何在anorm的“on”方法中动态创建列表?
我有一个带有可选输入的表单,目前我检查每个选项并创建一个定义了选项的列表,并尝试将其传递给anorm。目前我遇到了编译错误。
type mismatch; found : List[java.io.Serializable] required: (Any, anorm.ParameterValue[_])
我不确定该如何创建这个列表。
当前代码:val onList = List(
'school_id = input.school,
if(input.rooms isDefined) ('rooms -> input.rooms) else "None" ,
if(input.bathrooms isDefined) ('bathrooms -> input.bathrooms) else "None" ,
if(input.houseType isDefined) ('houseType -> input.houseType) else "None" ,
if(input.priceLow isDefined) ('priceLow -> input.priceLow) else "None" ,
if(input.priceHigh isDefined) ('priceHigh -> input.priceHigh) else "None" ,
if(input.utilities isDefined) ('utilities -> input.utilities) else "None"
).filter(_!="None")
SQL("SELECT * FROM Houses WHERE " + whereString).on(onList).as(sqlToHouse *)
最初我尝试做这件事是因为我认为它会和...一样,但现在我发现并非如此。
.on('rooms -> input.rooms, 'bathroom -> input.bathrooms... etc)
编辑:
代码现在为:
val onList = Seq(
('school_id -> input.school),
if(input.rooms isDefined) ('rooms -> input.rooms.get) else None ,
if(input.bathrooms isDefined) ('bathrooms -> input.bathrooms.get) else None ,
if(input.houseType isDefined) ('houseType -> input.houseType.get) else None ,
if(input.priceLow isDefined) ('priceLow -> input.priceLow.get) else None ,
if(input.priceHigh isDefined) ('priceHigh -> input.priceHigh.get) else None ,
if(input.utilities isDefined) ('utilities -> input.utilities.get) else None
).filter(_!=None).asInstanceOf[Seq[(Any,anorm.ParameterValue[_])]]
使用SQL命令:
SQL("SELECT * FROM Houses WHERE " + whereString).on(onList:_*).as(sqlToHouse *)
现在出现了异常
[ClassCastException: java.lang.Integer cannot be cast to anorm.ParameterValue]
whereString
看起来像什么? - Robin Green