1

我想执行以下操作:

我想返回一个用户列表,首先按用户“关注”的人排序,其次是一些额外的分数。但是,我在下面编写的以下代码不起作用,因为资助者是提升的 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 )  

所有帮助表示赞赏!

4

2 回答 2

3

您需要在 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))
于 2014-04-22T10:06:44.190 回答
-1

最终通过使用“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 )  
于 2014-04-22T01:21:57.097 回答