Reputation: 1624
I am trying to just return a single string from each object.
Given the following:
class Book {
String title
Date releaseDate
String author
Boolean paperback
}
for every instance of Book I want to get an array of authors then make them unique. I thought you could do something like:
def authors = Book.findAllByAuthor()
This just gives me an array off book objects. I know i can do a
a =[]
authors.each{a.add(it.author)}
a.unique()
I am almost certain there is a way just to grab all authors in one line.
any ideas?
Upvotes: 0
Views: 104
Reputation: 1502
This gives you distinct authors of any book:
Book.executeQuery("select distinct author from Book")
Upvotes: 1
Reputation: 24776
You can use projections to get a distinct list of authors across all books. Take a look at the createCriteria documentation for more examples.
def c = Book.createCriteria()
def authors = c.list() {
projections {
distinct('author')
}
}
Upvotes: 1