Realm Xamarin LINQ Object

后端 未结 1 1090
再見小時候
再見小時候 2021-01-21 16:36

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
{
          


        
相关标签:
1条回答
  • 2021-01-21 17:36

    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");
    

    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)'.

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

    Example of how I currently do it:

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

    Note: https://github.com/realm/realm-dotnet/issues/723

    0 讨论(0)
提交回复
热议问题