Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do a projection using a Grails 2.0 where query?

I like the new Grails 2.0 "where" queries but need to do a projection. Anyone know how? Right now I have code that instantiates all the domain instances and extracts the field I need:

List<Double> eloRatings = User.where { !deleted }.list()*.eloRating

This isn't very efficient.

like image 837
David Tinker Avatar asked Dec 27 '22 08:12

David Tinker


1 Answers

From this blog post, you can't use projections directly with where queries. However, since the returned object from a where query is a DetachedCriteria, you can append a traditional criteria to it, like so:

List<Double> eloRatings = User.where { !deleted }.projections {
    property 'eloRating'
}.list()

This should work, I tested it under Grails 2.0.

like image 173
OverZealous Avatar answered Jan 09 '23 05:01

OverZealous