I’m new to grails, but have previous experience using .net with c# and linq to query databases.
I’m trying to filter a list of objects by optional items using a multiple selection drop box. So to the controller I will get a list of parameters some of which will be null. So I want to have something akin to
DailyProduction.Where(x => loaction.contains(x.location)).Select().ToList().
However, it doesn’t see quite as simple with in groovy and grails.
Here is what I’ve tried:
def filteredList = DailyProductionReport.createCriteria()
def results = filteredList.list {
if(params.locationSelect != null)
'in'("location", [params.locationSelect.each{ it != null}])
}
But I get a Runtime Exception that says:
Class:
java.lang.ClassCastException
Message:
[Ljava.lang.String; cannot be cast to java.lang.String
I’ve tried look over different forums with out any luck. I’m almost at my wits end. If any groovy master can shed some light on things for me I’d greatly appreciate it.
Thanks
It looks like your problem might be
eachis used for iteration and the resulting expression is just the list being iterated over. Also, the square brackets nest that list in another list. You probably wantIn this particular case, you can also just use the identity closure since “groovy truth” for objects is the same as testing for null: