Efficient way to get EmbeddedObjects matching query?

Given two Realm classes

public class Note : EmbeddedObject
{
    [MapTo("_id")]
    public ObjectId Id { get; set; } = ObjectId.GenerateNewId();

    [MapTo("title")]
    public String Title { get; set; }
};

public class SharedWith : RealmObject
{
    
    [PrimaryKey]
    [MapTo("_id")]
    public ObjectId Id { get; set; } = ObjectId.GenerateNewId();

    [MapTo("ownerId")]
    public ObjectId OwnerId { get; set; }

    [MapTo("recipientId")]
    public ObjectId RecipientId { get; set; }  
    
    [MapTo("notes")]
    public IList<Note> Notes { get; }
};

How do I get a list from Realm realm of all the Note objects embedded within a set of SharedWith objects?

I can get the SharedWith objects with a query such as

 _realm.All<SharedWith>().Where(sw => sw.RecipientId == p.Id)

I want to have a flattened IEnumerable<Appointment> I can bind to a Xamarin CollectionView

I’m using a rather horrible instantion of all objects.

var myShared = _realm.All<SharedWith>().Where(sw => sw.RecipientId == p.Id);
var instNotes = new List<Note>();
foreach (var sw in myShared) {
	instNotes.AddRange(sw.Notes);
}
Notes = instNotes;

Hey Andy, so this is not really something well supported today. I guess a very hacky solution would be to use the internal AllEmbedded method and then filter that collection. It should look something like:

// Get a collection for all Note objects
var allNotes = (IQueryable<Note>)typeof(Realm).GetMethod("AllEmbedded", BindingFlags.Instance | BindingFlags.NonPublic)!
    .MakeGenericMethod(typeof(Note))
    .Invoke(realm, null)!;

// Filter using the backlinks property for notes where their SharedWith.recipientId is equal to p.Id
var filteredNotes = allNotes.Filter("@links.SharedWith.Notes.recipientId == $0", p.Id);

Thanks Nikola, I’ll have a go with the hacky solution but may not get to it for a fair while as lots of other stuff to deliver.

The code I showed above actually works surprisingly snappily, maybe because it’s doing purely local iterations. It’s part of my SyncOddly demo and is good enough for now that I can focus on other work. I suspect it may not scale though.