2013-09-27 21 views
1

當我在Play中讀取!文檔我找到一種方法,結果解析到一個List [(字符串,字符串)]檢索具有Scala anorm流的元組列表API

像這樣:

// Create an SQL query 
val selectCountries = SQL("Select * from Country") 

// Transform the resulting Stream[Row] as a List[(String,String)] 
val countries = selectCountries().map(row => 
row[String]("code") -> row[String]("name") 
).toList 

我想這樣做,但我的元組將包含更多的數據。

我在做這樣的:

val getObjects = SQL("SELECT a, b, c, d, e, f, g FROM table") 
    val objects = getObjects().map(row => 
    row[Long]("a") -> row[Long]("b") -> row[Long]("c") -> row[String]("d") -> row[Long]("e") -> row[Long]("f") -> row[String]("g")).toList 

每個元組我得到的將是這種格式着,當然,這就是我要請在上面的代碼:

((((((Long, Long), Long), String), Long), Long), String) 

但我想這一點:

(Long, Long, Long, String, Long, Long, String) 

什麼我問的是,我應該如何解析結果產生類似上面的最後一個元組。我想像他們在List [(String,String)]的文檔中那樣做,但使用更多的數據。

感謝

回答

2

你得到((((((Long, Long), Long), String), Long), Long), String)因爲->,每次調用此包裝兩種元素成一對。因此,與每個->你有一個元組,然後你把這個元組,並提出了新的,等你需要改變箭頭用逗號和包裝成()

val objects = getObjects().map(row => 
    (row[Long]("a"), row[Long]("b"), ..., row[String]("g")).toList 

但請記住,目前的元組可以沒有它超過22個元素。

+0

我知道' - >'是這樣的,我用'''嘗試了,而不是沒有運氣。但我現在看到你已經添加了'('和')',我沒有。馬上嘗試一下! – raxelsson

+0

謝謝@Alexlv,我錯過了'('和')'。感到很傻,錯過了這麼小的一件事。 – raxelsson