gpt4 book ai didi

linq - Realm Xamarin LINQ 对象

转载 作者:行者123 更新时间:2023-12-05 04:12:27 26 4
gpt4 key购买 nike

在查询包含来自其他 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com