Question

My table:

case class Subject(id: Int, name:String, describe: String, sub_resource:String, addId:Long, recommand:Int, commentsum :Int, commentnumber: Int, userId: Int)
class Subjects(tag: Tag) extends Table[Subject](tag, "Subject") {
  def id=column[Int]("ID", O.PrimaryKey)
  def name=column[String]("Name")
  def describe=column[String]("describe")
  def sub_resource=column[String]("Resource")
  def keywords=column[String]("Keyword")
  def addID=column[Long]("Address")
  def recommandrate=column[Int]("Recommand")
  def commentsum=column[Int]("Sum_of_rate")
  def commentnumber=column[Int]("Rate_number")
  def userId=column[Int]("owner")
  def uniqueName = index("idx_grp_name", name, unique = true)
  def * = (id, name,sub_resource,keywords, addID, recommandrate, commentsum, commentnumber,userId)<> (Subject.tupled, Subject.unapply)
  def sub_res=foreignKey("sub_res_FK", sub_resource, resource)(_.link)
  def sub_address=foreignKey("sub_add_FK", addID, address)(_.id)
  def sub_user=foreignKey("sub_user_FK", userId, user)(_.id)
}
val subject = TableQuery[Subjects]

I want get the list name contain "USA" and "China":

How to write the filter and list the name, userId and describe?

I want to use subject.filter(....).....

Was it helpful?

Solution

you need both a filter (to filter on USA and China) and a map to transform to name, userId, and decribe so something like (untested)

subject.filter(i => i.name === "USA" || i.name === "China")
       .map(i => (i._2, i._10, i._3) 

as the result is a list of tuples you have to access the fields with the ._[index] thingie

[update] I see not all your fields are in your def * = ... so that's also something to look at

Edited by cvogt: I corrected the Syntax. It's just like using Scala collections, except for the ===.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top