简体   繁体   中英

Playframework Slick DB Filter Operation

Consider following two strings as records, saved in column C1. S1: "MyValue1 - myValue2" S2: "MyValue1 - myValue2 - myValue3"

Following query will list above strings without any filter operations.

play.Logger.info("projectList: " + ((for { t <- Table } yield t).map{_.C1}.list))

Question: I want to filter the list by number of hyphens ('-')

I tried following query but It's not working correctly

play.Logger.info("projectList: " + ((for { t <- Table if (t.C1.toString().split("-").length == someLength } yield t).map{_.C1}.list))

where someLength equals either 2 or 3. Any idea of using string operations as filter in slick DB for playframework?

toString is not a Slick method. Once you use it you leave the realm of database queries and operate on the client side in a way you are probably not expecting. Here are all supported methods: http://slick.typesafe.com/doc/2.1.0/api/#scala.slick.lifted.StringColumnExtensionMethods

You can probably do it like this:

.filter(t => (t-size - t.replace("-","").size) === someLength)

I used .filter to use string operations. Thanks @cvogt for the help though.

Solution:

((for { t <- Table } yield t).list.filter{ t => t.split("-").length == someLength})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM