Scala SLICK:如何在另一个查询中使用查询结果?
我想执行如下操作: 我想返回一个用户列表,首先按用户的“跟随者”排序,其次按一些额外的分数排序。 但是,我在下面编写的代码不起作用,因为资助者是被提升的滑头类型,因此在列表中找不到Scala SLICK:如何在另一个查询中使用查询结果?,scala,playframework-2.0,slick,play-slick,slick-2.0,Scala,Playframework 2.0,Slick,Play Slick,Slick 2.0,我想执行如下操作: 我想返回一个用户列表,首先按用户的“跟随者”排序,其次按一些额外的分数排序。 但是,我在下面编写的代码不起作用,因为资助者是被提升的滑头类型,因此在列表中找不到 //The following represents the query for only funders who we are following val following_funders: List[User] = ( for {
//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 )
//下面的查询只针对我们关注的出资人
val下列出资人:列表[用户]=(
为了{
funder最终通过使用“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 )
//下面的查询只针对我们关注的出资人
val以下资金方ID:List[Long]=(
为了{
funder您需要在Slick中使用ID(即主键)。这就是在db端唯一标识对象的方式。您不需要执行第一个查询。您可以将其用作第二个查询的一个组件,而无需首先使用
中的操作符执行它:
//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 )
scala> List( (1,"b"), (2,"a") ).sortBy(_._1).sortBy(_._2)
res0: List[(Int, String)] = List((2,a), (1,b))