我想执行以下操作:
我想返回一个用户列表,首先按用户"关注"的人排序,其次按一些额外的分数排序。然而,我在下面写的以下代码不起作用,因为资助者是提升的Slick类型,因此从未在列表中找到。
//The following represents the query for only funders who we are following
val following_funders: List[User] = (
for {
funder <- all_funders
f <- follows if f.followerId === id //get all the current users follower objects
if f.followeeId === funder.id
} yield funder
).list
val all_funders_sorted = for {
funder <- all_funders
following_funder = following_funders contains funder
} yield (funder, following_funder)
//sort the funders by whether or not they are following the funder and then map it to only the funders (i.e. remove the boolean)
all_funders_sorted.sortBy(_._2.desc).sortBy(_._1.score.desc).map( x => x._1 )
感谢所有的帮助!
您需要在Slick中使用id(即主键)。这就是对象在数据库端唯一标识的方式。您不需要执行第一个查询。您可以将其用作第二个组件,而无需首先使用in
运算符执行:
//The following represents the query for only funders who we are following
val following_funders_ids = (
for {
funder <- all_funders
f <- follows if f.followerId === id //get all the current users follower objects
if f.followeeId === funder.id
} yield funder.id
val all_funders_sorted = for {
funder <- all_funders
following_funder = funder.id in following_funders_ids
} yield (funder, following_funder)
//sort the funders by whether or not they are following the funder and then map it to only the funders (i.e. remove the boolean)
all_funders_sorted.sortBy(_._1.impactPoints.desc).sortBy(_._2.desc).map( x => x._1 )
如果你第一次想按以下方式排序,请注意你的排序顺序是错误的。Slick将.sortBy(_.a).sortBy(_.b)
转换为ORDER BY B,A
,因为Scala集合就是这样工作的:
scala> List( (1,"b"), (2,"a") ).sortBy(_._1).sortBy(_._2)
res0: List[(Int, String)] = List((2,a), (1,b))
最终通过使用"inSet"以以下方式解决了问题
//The following represents the query for only funders who we are following
val following_funders_ids: List[Long] = (
for {
funder <- all_funders
f <- follows if f.followerId === id //get all the current users follower objects
if f.followeeId === funder.id
} yield funder.id
).list
val all_funders_sorted = for {
funder <- all_funders
following_funder = funder.id inSet following_funders_ids
} yield (funder, following_funder)
//sort the funders by whether or not they are following the funder and then map it to only the funders (i.e. remove the boolean)
all_funders_sorted.sortBy(_._2.desc).sortBy(_._1.impactPoints.desc).map( x => x._1 )