我想执行以下操作:
我想返回一个用户列表,该列表首先按用户“关注”的人排序,然后按一些额外的分数得分。但是,我在下面编写的以下代码不起作用,因为资助者是提升的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))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句