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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With