gpt4 book ai didi

scala - 光滑的过滤器或不再支持逻辑操作的地方?

转载 作者:行者123 更新时间:2023-12-04 23:01:15 25 4
gpt4 key购买 nike

我使用 slick 2.0.2,我只想做一个简单的过滤器或使用 where 子语句,我只想在过滤器中执行诸如“and”、“or”和“not”之类的逻辑操作:

val subjectdata = TableQuery[SubjectTable]
...
subjectdata.where(i=>(i.id===id && i.userId===rs.user.get.identityId.userId)).list()

并得到错误:
[error] G:\testprojects\slickplay\app\controllers\ShopController.scala:89: Cannot perform option-mapped operation
[error] with type: (Long, String) => R
[error] for base type: (Long, Long) => Boolean
[error] subjectdata.where(i=>(i.id===id && i.userId===rs.user.get.identityId
.userId)).list()
[error]
^

在光滑的 1.0.1 我可以做:
val results = Query(TableClass)
.filter(r => r.isNull || r.expires > new Timestamp(DateTime.now().getMillis()))
.list

我想在 Slick2 中的 TableQuery 上做类似的事情。怎么做?

最佳答案

需要知道的一件事是,Slick 的操作对类型的要求比 Scala 更严格。两个操作数都必须具有相同的基类型,并可选择包装在选项中。因此,将 Double 与 Double 或 Option[Double] 进行比较是可以的,但将其与 Int 进行比较会给您这样的编译时警告。错误消息提示您解决问题

[error] G:\testprojects\slickplay\app\controllers\ShopController.scala:89: Cannot perform option-mapped operation
[error] with type: (Long, String) => R
[error] for base type: (Long, Long) => Boolean
[error] subjectdata.where(i=>(i.id===id && i.userId===rs.user.get.identityId
.userId)).list()

(Long, String) => R您会看到参数没有匹配的类型并且无法确定返回类型。所以我假设 idrs.user.get.identityId是一个字符串。转变成 Int使用 .toInt .或者,您可以使用 .asColumnOf[String] 转换 db-side 值.

关于scala - 光滑的过滤器或不再支持逻辑操作的地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23877451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com