Search code examples
grailsgroovygrails-orm

Best way to compare lists to fields on a list of objects in Groovy?


So I have a list:

def list = [1,2,3,...]

And then I have a list of objects from the database:

def loo = findAllBySomeField()

And for each object in the list, if field A on that object doesn't match anything in the first list, then I want to add field B on the object to another list. What's the best way to do this without having a bunch of .each and .collect closures? I've looked into intersect() and removeAll() but it seems there's no clean and simple way to do this.

Also is there a findAllBySomeFieldNotInList()? It seems grails only has the InList dynamic method but no NotInList().


Solution

  • createCriteria().list() {
        not {
            inList 'someField', unwantedValues
        }
        projections {
            property 'fieldB' // or distinct instead of property
        }
    }
    

    should do