.netmongodbrealmmongodb-atlasrealm-mobile-platform

Query a field in an embedded document in MongoDb that throws "The left-hand side of the Call operator must be a direct access to a persisted property"


I have an embedded document in Atlas, where I am trying to query a field (i.e. document.ProfileTab.AadharNo) from Realm, but getting Exception: System.NotSupportedException: 'The left-hand side of the Call operator must be a direct access to a persisted property in Realm. Unable to process 'c.ProfileTab.AadharNo'.'

From Realm sdk, I am trying to query the field like this:

var v = App.realmInstance.All<App.ArogyaMainClass>().Where(
    c => !string.IsNullOrEmpty(c.ProfileTab.AadharNo) 
    && 
    c.ProfileTab.AadharNo.Equals(entryAadharNo.Text.Trim())
    )
    .ToList();

Is creating an index on the field will help? If Realm is expecting persisted field, what it is and how to make the field persisted? Do I need to set [required] on the field? Plz suggest some suggestions. Thanks in advance.

Below are the Realm Object Models:

        public class ArogyaMainClass : RealmObject
    {
        [MapTo("_id")]
        [PrimaryKey]
        public ObjectId? Id { get; set; } = ObjectId.GenerateNewId();
        [MapTo("profileTab")]
        public ArogyaProfileTab ProfileTab { get; set; }
    }


        public class ArogyaProfileTab : EmbeddedObject
    {
        [MapTo("PatientName")]
        public string PatientName { get; set; }
        [MapTo("AadharNo")]
        public string AadharNo { get; set; }
        [MapTo("PhoneNo")]
        public string PhoneNo { get; set; }
    }

Solution

  • Unfortunately the LINQ implementation of the .NET Realm SDK does not yet support querying on linked objects. In order to achieve what you showed, you need to use Filter. The language used in the filter method is called Realm Query Language.

    The query for your need would look like

    var v = App.realmInstance.All<App.ArogyaMainClass>().Filter("ProfileTab.AadharNo.@count > 0 AND ProfileTab.AadharNo == $0", entryAadharNo.Text.Trim()).ToList();