6

这个问题与另一个有关。我还尝试使用 joinLeft 对查询进行排序,但在 slick 3.0.0 中。由于 Option Rep 被自动解除,我将如何做同样的事情?:

def list(filter: String, orderBy: Int):Future[Seq[(Computer, Option[Company])]] = {
    val initialQuery = for {
        (computer, company) <- Computer.filter(_.name like filter) leftJoin 
            Company on (_.companyId === _.id)
    } yield (computer, company)

    val sortedQuery = orderBy match {
        case 2 => initialQuery.sortBy(_._1.name) //Works ok, column from a primary table
        case 3 => initialQuery.sortBy(_._2.map(_.name)) //could not find implicit value for parameter ol: slick.lifted.OptionLift[slick.lifted.ColumnOrdered[String],slick.lifted.Rep[Option[QO]]]
    }
    db.run(sortedQuery.result)
}

谢谢,

4

4 回答 4

5

我想缺少括号只是一个错字。最近,当我使用您的示例在错误的位置指定排序方向时,我遇到了这个问题:

case 3 => initialQuery.sortBy(_._2.map(_.name.asc))

它应该是:

case 3 => initialQuery.sortBy(_._2.map(_.name).asc)
于 2015-10-22T13:31:50.433 回答
1

我也有这个问题。我有一个 joinLeft,我想在一个布尔列上订购。您必须决定当它的连接为空时,您要替换它的值。

例如:

initialQuery.sortBy(_._2.map(_.name).getOrElse(""))
于 2018-08-15T11:26:52.240 回答
0

你确定你复制了正确的代码?

case 3 => data.sortBy(_._2.map(_.name) //could not find implicit value for parameter

缺少一个“)”

应该

case 3 => data.sortBy(_._2.map(_.name))
于 2015-09-16T22:07:05.963 回答
0

您可以将字段放入结果集中。例如:

 val initialQuery = for {
        (computer, company) <- Computer.filter(_.name like filter) leftJoin Company on (_.companyId === _.id)
        } yield (computer, company, company.map(_.name))

    val sortedQuery =  orderBy match {
        case 2 => initialQuery.sortBy(_._1.name) 
        case 3 => initialQuery.sortBy(_._3)
    }

    db.run(sortedQuery.map(v => (v._1, v._2).result)
于 2016-08-18T13:12:03.477 回答