【问题标题】:How to apply Java lambda expression filtering in Grails如何在 Grails 中应用 Java lambda 表达式过滤
【发布时间】:2016-07-15 09:07:44
【问题描述】:

我已经为我的 spring mvc 控制器编写了一些代码,用于使用 lambda 表达式过滤器从我的表中的单个列中获取员工出勤记录。 但我想在我的 Grails 控制器中应用相同的逻辑。

看看 scree。

谢谢

【问题讨论】:

    标签: hibernate spring-mvc grails grails-orm


    【解决方案1】:

    在您的情况下,迭代同一个集合 4(!!!)次并产生 4 个新集合的直接方法也是性能最低的方法。

    我会这样说:

    import static Constant.*
    
    def data = presentMonthAtten.inject( [:].withDefault{ 0 } ){ accum, record ->
      switch( record.status ){
        case ABSENT: accum.absent++; break
        case PRESENT: accum.present++; break
        // other cases
      }
      accum
    }
    
    //....
    attendDashBoard.currentMonthAbsentAttn = data.absent.toString()
    

    【讨论】:

    • 哇..我要实现这个,如果我遇到任何问题,我会告诉你并指导我。
    • 这里出现异常,没有这样的属性:类的 materialStatus:java.lang.String=== poStatus = PurchaseOrder.executeQuery('select materialStatus from PurchaseOrder') def data = poStatus.inject( [:].withDefault{ 0 } ){ accum, record -> switch(record.materialStatus ){,在我的采购订单中,有可变的 materialStatus。
    • 您正在尝试访问 String 对象上的属性 materialStatus。看起来你应该使用switch( record ){ ... }
    • 嗨@injecteer,请检查此链接,如果可能,请帮助我。 stackoverflow.com/questions/38443269/…
    【解决方案2】:

    如果我理解正确(计算记录),您可以尝试以下操作:

    long totalAbsentDays 
    = presentMonthAtten.findAll{ it.status == Constant.ABSENT }.size()
    

    【讨论】:

    • 看起来不错,但花括号后面的“它”是什么意思?
    • 它是默认的迭代器变量,它是自动可用的。您也可以通过 'presentMonthAtten.findAll{ record -> record.status == Constant.ABSENT }.size()' 自行定义迭代器变量的名称
    • 好吧,我会实现它并让你知道。
    猜你喜欢
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多