简体   繁体   English

Playframework Slick DB筛选器操作

[英]Playframework Slick DB Filter Operation

Consider following two strings as records, saved in column C1. 考虑将以下两个字符串作为记录,保存在列C1中。 S1: "MyValue1 - myValue2" S2: "MyValue1 - myValue2 - myValue3" 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? 其中someLength等于2或3。在光滑的DB中使用字符串操作作为过滤器进行播放框架的想法吗?

toString is not a Slick method. toString不是Slick方法。 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 以下是所有受支持的方法: 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. 我使用.filter来使用字符串操作。 Thanks @cvogt for the help though. 感谢@cvogt的帮助。

Solution: 解:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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