领域Xamarin LINQ对象
使用LINQ查询领域的正确方法是什么,其中查询包括来自其他领域对象的字段?例如:领域Xamarin LINQ对象,linq,xamarin,realm,Linq,Xamarin,Realm,使用LINQ查询领域的正确方法是什么,其中查询包括来自其他领域对象的字段?例如: public class Department : RealmObject { [Primary Key] public string UniqueId { get; set; } } public class Employee : RealmObject { [Primary Key] public string Name { get; set; } // Parent
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 );
var employee=realm.All();
但这总是不返回匹配项。Where()也不返回匹配项。然而,取消e.Department并只搜索员工姓名可以很好地工作,但显然不能按预期范围搜索部门
这是最新的领域Xamarin 0.80
我做错了什么?当前不支持通过嵌套的
RealmObjects
属性进行查询:
var deptFilter = theRealm.ObjectForPrimaryKey<Department>("HR");
var employeesByDept = theRealm.All<Employee>().Where((Employee emp) => emp.Department == deptFilter & emp.Name == "StackOverflow");
这里要澄清的是,我们还不支持像这样的相关对象的查询。我们会在未来,但目前没有时间表
以下内容目前也不受支持:
var deptFilter = theRealm.ObjectForPrimaryKey<Department>("HR");
var employeesByDept = theRealm.All<Employee>().Where((Employee emp) => emp.Department == deptFilter & emp.Name == "StackOverflow");
注意:非常好,谢谢。比我希望的要麻烦一点,但确实奏效了。我现在意识到,在很多这样的情况下,我可以直接在某个department.Employees上迭代来搜索匹配项,而不是运行查询。我假设这比LINQ查询要快?@BSohl只有通过执行
.ToList
,.ToArray,foreach(…in…{}
)等实际访问RealmObjects
,才会计算“查询”。。。因此,对emp的名称进行子查询要比对部门中的所有员工执行foreach
更快,因为在C#中进行相等比较时,必须访问每个对象,而在领域.Core
(C++)级别进行相等比较时,则必须访问每个对象。就我个人而言,我总是尽可能地过滤到最小的数据集,以获得最快的响应和最小的内存开销,然后将那些RealmObjects
带到属性访问的C级