列表<list>上的Groovy findAll()给出错误</list>

时间:2013-12-02 09:51:04

标签: grails groovy

我正在尝试运行代码并插入一段代码,我发现它非常简单:

List<List> nearestAvailableEmployees = Staffing.createCriteria().list {
    createAlias('employee','e')
    projections {
        property('e.id')
        property('e.name')
        property('endDate')
        property('startDate')
    }
    gt('endDate', new Date())
    and{
        order("e.id", "asc")
        order("endDate", "asc")

       }
}

return nearestAvailableEmployees.findAll{
    it[0] == 112
 }

在运行上面的代码时,我收到以下错误,但我没理由。

groovy.lang.MissingMethodException: No signature of method:
Script1$_run_closure2.doCall() is applicable for argument types: 
(java.lang.Long, java.lang.String, java.sql.Timestamp, java.sql.Timestamp) values:
[5, Aditee Chatterjee, 2013-12-27 00:00:00.0, ...]
Possible solutions: doCall(), call(), doCall(java.lang.Object), findAll()
at Script1.run(Script1.groovy:26)
at org.grails.plugins.console.ConsoleService.eval(ConsoleService.groovy:57)
at org.grails.plugins.console.ConsoleService.eval(ConsoleService.groovy:37)
at org.grails.plugins.console.ConsoleController$_closure2.doCall(ConsoleController.groovy:61)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744) undefined

我通过修改代码并在条件查询之后添加以下代码段来纠正错误:

.collect{
    it as List
}

任何人都可以帮我处理代码。

1 个答案:

答案 0 :(得分:0)

使用此代码:

List<List> nearestAvailableEmployees = Staffing.createCriteria().list {
...
}

您将获得Staffing对象列表,而不是列表列表。 请注意,Groovy中的Generics可能对代码可读性很好,但即使在编译时也没有影响(与Java不同),正如您的代码编译成功所证明的那样。

所以你可以将find closure更改为:

return nearestAvailableEmployees.findAll{
    // it is of type Staffing 
    it.id == 112
}

或者强调nearestAvailableEmployees是Staffing对象的列表:

return nearestAvailableEmployees.findAll{
    Staffing staffing ->

    staffing.id == 112
}
相关问题