【问题标题】:QueryDsl reduce expression streamQueryDsl 减少表达式流
【发布时间】:2022-01-21 22:13:30
【问题描述】:

这里是我的 querydsl 表达式流:

Stream.of(
    Pair.of(QPatient.patient.name.any().given, Optional.ofNullable(given)),
    Pair.of(QPatient.patient.name.any().family, Optional.ofNullable(family))
)
.filter(pair -> Objects.nonNull(pair.getValue()));

我想创建一个BooleanExpression

我尝试使用BooleanBuilder,但我不太清楚如何将流项目收集和减少到单个BooleanExpression

简短地说,没有流:

BooleanBuilder booleanBuilder = new BooleanBuilder();
booleanBuilder.and(booleanExpression1).and(booleanExpression2)...

有什么想法吗?

【问题讨论】:

    标签: java-stream querydsl


    【解决方案1】:

    您可以在Stream.reduce 中使用BooleanBuilder,如下所示:

    BooleanBuilder reduce = Stream.of(
                QPatient.patient.id.isNull(),
                QPatient.patient.id.isNull()
            ).reduce(new BooleanBuilder(), BooleanBuilder::and, BooleanBuilder::and);
    

    这对您不起作用的原因是BooleanBuilder 只接受谓词,而QPair 不是谓词(实际上,甚至不是布尔表达式)。

    还要注意Optional.ofNullable 永远不会产生空值,从而使您的过滤器无用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-17
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多