【问题标题】:Realm to many query not live领域对许多查询不存在
【发布时间】:2018-07-03 19:01:55
【问题描述】:

我有两个模型 Parent 和 Child(一对多),Parent 包含 IList Children。

在我的视图模型中,我有一个绑定到属性 ChildList 的 ListView。

ChildList = _dataService.Realm.All<Parent>().First(d => d.ParentId == "MyParentIdentifier").Children;

将产生实时查询(如果我向 IList 添加或删除 Child,它将反映在 UI 中)。

ChildList = _dataService.Realm.All<Parent>().First(d => d.ParentId == "MyParentIdentifier").Children.Where(d => d.ChildName.StartsWith("A"));

不会产生实时查询。 UI 只会在关闭并重新打开页面后更新。

如何通过此设置创建实时查询?我还尝试向 Child 模型添加反向链接属性和如下查询:

ChildList = _dataService.Realm.All<Child>().Where(d => d.Parent.ParentId == "MyParentIdentifier");

但这会导致崩溃并出现错误:

System.NotSupportedException:Equal 运算符的左侧 必须是对 Realm 中持久属性的直接访问。无法 处理“d.Parent.ParentId”。

【问题讨论】:

标签: c# .net xamarin.forms realm


【解决方案1】:

Realm .NET 目前不支持遍历属性 (d.Parent.ParentId),但它支持对象比较:

var parent = _dataService.Realm.Find<Parent>("MyParentIdentifier");
ChildList = _dataService.Realm.All<Child>().Where(c => c.Parent == parent);

【讨论】:

  • var parent = _dataService.Realm.All().First(d => d.MyParentString = "我想要什么"); System.Diagnostics.Debug.WriteLine(parent.MyParentString); ChildList = _dataService.Realm.All().Where(d => d.Parent == parent);不太一样,但我认为等效-打印语句将返回正确的值,但子列表查询会给我与原始帖子相同的错误。我哪里错了?
  • 您使用的是什么版本的库?这绝对应该适用于最新的。
  • 我不得不降级到 1.5,但是在最新的稳定版上进行测试时,我仍然遇到同样的错误。我在模型中的反向链接代码只是 [Backlink(nameof(Parent.Children))] public IQueryable&lt;Parent&gt; Parents { get; } public Parent Parent =&gt; Parents.SingleOrDefault();
  • Parent 不受 Realm 管理,这就是您收到此错误的原因。您应该将 Parent.Children 切换为反向链接属性,但这意味着每个孩子只能有 1 个父级。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多