【问题标题】:Realm: combine "or" and "and"境界:结合“或”和“与”
【发布时间】:2019-01-28 18:42:52
【问题描述】:

我正在寻找一种在领域查询中组合 AND 和 OR 的方法。

这是我的课:

class Event extends RealmObject {
   String id;
   String date;
}

现在,我有一个 ids 列表,我想检索 ID 为 {1、4、5} 和 date = "2016-01-01"Events。在 SQL 中,我会有这样的查询:WHERE id IN ids AND date = 'mydate'

到目前为止我已经尝试过:

RealmQuery<Event> query = realm.where(Event.class);

for(String id : ids) {
   query = query.or().equalTo("id", id);
}

query.equalTo("date", "2016-01-01");

但它不会那样工作。我如何在 Realm 中做到这一点?

【问题讨论】:

  • 尝试使用循环来构建类似realm.where(Event.class).beginGroup().equalTo("id", 1).or().equalTo("id", 2).endGroup().equalTo("date", "2016-01-01");的查询

标签: realm


【解决方案1】:

这里是解决方案。感谢您将我指向“群组:

RealmQuery<Event> query = realm.where(Event.class);
int x = 0
for(String id : ids) {
   if (x != 0) {
     query.or();
   }
   query.beginGroup();
   query.equalTo("id", id);
   query.equalTo("date", "2016-01-01");
   query.endGroup();
   x++;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多