Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails GORM - Find all objects with distinct property value

I'm using Grails and I'm trying to figure out how to get all the distinct values of a domain class' property. In other words I'm looking for all the distinct values for a column. However, I'm not sure how to translate this into a GORM statement, preferable a criteria statement.

I'm open to either something that gives me a list of domain classes (with each having a distinct value for the property in question) and I collect all the properties values. Or something that gives me the distinct property values directly.

like image 711
Harry Muscle Avatar asked Nov 25 '14 19:11

Harry Muscle


1 Answers

I think all you need to accomplish this is to use projections and distinct. According to the documentation this will provide you with a list of distinct values of a property from your domain class.

def results = MyDomain.withCriteria {
  projections {
    distinct("theDistinctProperty")
  }
}
like image 132
Joshua Moore Avatar answered Sep 23 '22 00:09

Joshua Moore