linq - Realm Xamarin LINQ 对象

标签 linq xamarin realm

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

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; }
}

然后我希望能够做类似的事情:

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

但这总是不返回任何匹配项。 Where() 也不返回任何匹配项。但是,删除 e.Department 并仅搜索员工姓名可以正常工作,但显然不符合预期的部门范围。

这是最新的 Realm Xamarin 0.80。

我做错了什么?

最佳答案

目前不支持通过嵌套的 RealmObjects 属性进行查询:

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.

目前还支持以下内容:

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

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 表达式中,所以将其进一步分解为子查询。

我目前的做法示例:

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

关于linq - Realm Xamarin LINQ 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40330887/

相关文章:

android - 编译项目时出错

swift - 在 Swift 中从捆绑的 Realm 数据库加载数据

ios - Realm 迁移没有充分记录。谁能澄清一下?

c# - 将 IQueryable 转换为 IOrderedQueryable

C# linq FirstOrDefault() 函数

android绑定(bind)库 protected 成员

ios - 基于另一个属性值的属性默认值

c# - linq - 如何查找一个集合的键列是否是另一个集合的子集?

c# - 此 SQL 语句的等效 LINQ to SQL 查询表达式是什么?

c# - 在 C# 中的 Android Activity 之间传递自定义对象