Scala 如何从运行时参数中按列指定SLICK查询排序?

Scala 如何从运行时参数中按列指定SLICK查询排序?,scala,scalaquery,slick,Scala,Scalaquery,Slick,我使用下面的灵活查询来获取数据表的分页结果,该数据表的name字段与一些值条件匹配,并按name列排序 val q = ThirdParties.where(_.name like criteria).sortBy(_.name.asc.nullsLast).drop(offset).take(pageSize) val thirdParties = (for(s <-q) yield(s)).list map { case t: ThirdParty => t } val q=T

我使用下面的灵活查询来获取数据表的分页结果,该数据表的name字段与一些值条件匹配,并按name列排序

val q = ThirdParties.where(_.name like criteria).sortBy(_.name.asc.nullsLast).drop(offset).take(pageSize)
val thirdParties = (for(s <-q) yield(s)).list map { case t: ThirdParty => t }
val q=ThirdParties.where(u.name-like-criteria).sortBy(uu.name.asc.nullsLast).drop(offset).take(pageSize)
val thirdParties=(对于(s t}
这对我来说还可以,但现在我需要能够将运行时参数传递给sortBy方法,该方法标识要执行排序依据的列。
我调用查询的方法将有一个int,它表示数据表中列的索引


如何从int列索引获取sortBy方法所需的必要类型?

这样做会失去一些类型安全性,但这样的方法可能伤害最小:

这是光滑文档中的咖啡示例 希望列的一个子集由“index”寻址。在我们的示例中,让我们 由于某种原因,我们有两列价格,还有销售 我们称之为列
0
1
2
的列。 . 如果你能忍受轻微的干裂,比如:

object Coffees extends Table[(String, Int, Double, Double, Int, Int)]("COFFEES") {
  def name = column[String]("COF_NAME", O.PrimaryKey)
  def supID = column[Int]("SUP_ID")
  def price1 = column[Double]("PRICE1")
  def price2 = column[Double]("PRICE2")
  def sales = column[Int]("SALES")
  def total = column[Int]("TOTAL")
  def * = name ~ supID ~ price1 ~ price2 ~ sales ~ total
  def nth = Vector(price1, price2, sales) // Your index-addressable columns 
}
这里的
coffee.nth
是一个列向量,包含
Int
Double

scala> Coffees.nth
scala.collection.immutable.Vector[scala.slick.lifted.Column[_ >: Int with Double <: AnyVal]] = Vector(COFFEES.PRICE1, COFFEES.PRICE2, COFFEES.SALES)
然后调用查询

 val colIndx: Int = // gotten at runtime
 val thirdParties = (for(s <-q(colIndx)) yield(s)).list map { case t: ThirdParty => t }
val colIndx:Int=//在运行时获取
val thirdParties=(对于(s t}

Slick还是ScalaQuery?语义在Slick中发生了变化
 val colIndx: Int = // gotten at runtime
 val thirdParties = (for(s <-q(colIndx)) yield(s)).list map { case t: ThirdParty => t }