我有两个表, ProductMapper,产品。 ProductMapper具有产品的到期时间。
List<Product>
在mapper.expiry
new Date()
大于productIds
的{{1}}
ProductMapper
import java.util.Date;
import io.realm.RealmList;
import io.realm.RealmObject;
public class ProductMapper extends RealmObject {
private Date expiry;
private RealmList<Product> products;
// Setter, Getter
}
产品
import io.realm.RealmObject;
import io.realm.RealmResults;
import io.realm.annotations.LinkingObjects;
public class Product extends RealmObject {
@PrimaryKey
private int id;
private String name;
private boolean enabled;
@Nullable
@LinkingObjects("products")
private final RealmResults<ProductMapper> mapper = null;
}
expiry
。 productIds
的ProductMapper列表。 productIds
productAvail
以下是查找未过期但没有重复的产品的查询。
@NonNull
public static RealmList<Product> getValidProducts(@NonNull long[] productIds) {
Realm realm = Realm.getDefaultInstance();
RealmQuery<ProductMapper> query = realm.where(ProductMapper.class);
// Greater than current time.
query.greaterThanOrEqualTo("expiry", new Date());
query.beginGroup();
for (long productId : productIds) {
query.equalTo("products.id", productId).or();
}
// To bypass "Missing right-hand side of OR" Exception, caused by the above loop
query.equalTo("products.id", -1);
query.endGroup();
RealmList<Product> productAvail = new RealmList<>();
for (ProductMapper productMapper : query.findAll()) {
RealmQuery<Product> resQuery = productMapper.getProducts().where();
// Start Group
resQuery.beginGroup();
for (long storeId : productIds) {
resQuery.equalTo("id", storeId).or();
}
// To bypass "Missing right-hand side of OR" Exception, caused by the above loop
resQuery.equalTo("id", -1);
// End Group
resQuery.endGroup();
// Start Group, this group removes already found products.
resQuery.beginGroup();
for (Product product : productAvail) {
resQuery.notEqualTo("id", product.getId());
}
// End Group
resQuery.endGroup();
RealmResults<Product> prd = resQuery.findAll();
productAvail.addAll(realm.copyFromRealm(prd));
// check if we have got all the products.
if (productAvail.size() == productIds.length) {
break;
}
}
realm.close();
return productAvail;
}
有没有办法通过有效的查询找到相同的内容?
如果我从孩子到父母和孩子的搜索,我可能最终会得到重复的内容。然后回到孩子身边。正如这里提到的https://github.com/realm/realm-java/issues/5665#issuecomment-357264095
答案 0 :(得分:0)
1。)df.set_index(['Date', 'Business'], inplace=True)
df = df.reindex([('2016-12-31', 'FRC'), ('2017-03-31', 'FICC'), ('2017-06-30', 'FRC'), ('2017-02-15', 'FICC'), ('2017-03-31', 'FRC')])
df.sort_index(level=[1, 0], inplace=True)
df.groupby('Business')['Amount'].transform(lambda x: x.interpolate()).to_frame()
Out[6]:
Amount
Date Business
2017-02-15 FICC 200.0
2017-03-31 FICC 100.0
2016-12-31 FRC 10.0
2017-03-31 FRC 15.0
2017-06-30 FRC 20.0
甚至不应该存在。它只是一个奇特的RealmObject类,让你的生活变得悲惨,而不是在ProductMapper
中有一个Date
字段。
2。)在Realm 3.5.0+中,您的查询应该是
Product