如何使用Grails 2.0进行投影查询?

Dav*_*ker 4 grails grails-orm

我喜欢新的Grails 2.0"where"查询,但需要进行投影.谁知道怎么样?现在我有代码实例化所有域实例并提取我需要的字段:

List<Double> eloRatings = User.where { !deleted }.list()*.eloRating
Run Code Online (Sandbox Code Playgroud)

这不是很有效.

Ove*_*ous 10

此博客文章中,您无法直接where查询使用投影.但是,由于where查询返回的对象是a DetachedCriteria,您可以向其添加传统条件,如下所示:

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

这应该工作,我在Grails 2.0下测试它.