领域Xamarin LINQ对象 [英] Realm Xamarin LINQ Object

查看:67
本文介绍了领域Xamarin LINQ对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查询中包含来自其他Realm对象的字段的情况下,使用LINQ查询Realm的正确方法是什么?例如:

What is the correct way to query Realm with LINQ where the query includes fields from other Realm objects? For example:

public class Department : RealmObject
{
    [Primary Key]
    public string UniqueId { get; set; }
}

public class Employee : RealmObject
{
    [Primary Key]
    public string Name { get; set; }

    // Parent
    public Department Department { get; set; }
}

然后,我希望能够执行以下操作:

Then I would expect to be able to do something like:

var employee = realm.All<Employee>().SingleOrDefault( e => e.Department.UniqueId == fooId && e.Name == fooName );

但这始终不返回任何匹配项. where()也不会返回任何匹配项.但是,删除e.部门并仅搜索员工姓名可以很好地工作,但显然不能按预期适用于Department.

But this always returns no matches. Where() also returns no matches. However, eliminating the e.Department and searching only on employee name works fine but obviously does not scope to Department as intended.

这是最新的Realm Xamarin 0.80.

This is with the latest Realm Xamarin 0.80.

我在做什么错了?

推荐答案

当前不支持通过嵌套的RealmObjects属性查询:

Querying by nested RealmObjects attributes is not currently supported:

仅在此处进行说明,我们尚不支持对此类相关对象的查询.我们会在将来,但目前没有时间表.

Just to clarify here, we don't yet support queries on related objects like this. We will in the future, but there is no timeline at the moment.

当前还不支持以下内容:

The following is also not currently supported:

var deptFilter = theRealm.ObjectForPrimaryKey<Department>("HR");
var employeesByDept = theRealm.All<Employee>().Where((Employee emp) => emp.Department == deptFilter & emp.Name == "StackOverflow");

And运算符的左侧必须是对Realm中持久化属性的直接访问. 无法处理'(emp.Department == value(Realm080.App + c__AnonStorey1).deptFilter)'.

The left-hand side of the And operator must be a direct access to a persisted property in Realm. Unable to process '(emp.Department == value(Realm080.App+c__AnonStorey1).deptFilter)'.

您可以直接进行RealmObject等式,只是不能使用相同的Linq表达式,因此可以将其进一步细分为子查询.

You can do direct RealmObject equalities, just not in the same Linq expression, so break it down further into a sub-query.

var deptFilter = theRealm.ObjectForPrimaryKey<Department>("HR");
var employeesByDept = theRealm.All<Employee>().Where((Employee emp) => emp.Department == deptFilter);
var employees = employeesByDept.Where((Employee emp) => emp.Name == "StackOverflow");
foreach (var emp in employees)
{
    D.WriteLine(emp.Name);
}

注意: https://github.com/realm/realm-dotnet/issues/723

这篇关于领域Xamarin LINQ对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆