C# .Net SDK - how to remove a key from a dictionary along with the value

I can not seem (despite lots of searching) to find any help with deleting a key from a Realm dictionary (along with value) with the .NET Realm SDK. What I have managed is to delete the value using realm.Remove(), but it leaves a key with the value as null in the Realm collection. What I am currently doing …

realm.Write(() =>
{
var dict = realm.All<my_dict>();
var my_val = dict[my_key];
realm.Remove(my_val);
}

which leaves the key still in the dictionary, and I would also like the key removed. I feel a little silly, like it is staring me in the face! The closest I could find searching was the $unset keyword, but do not see an equivalent in the .NET Realm SDK.

Thanks in advance for helping me with this silly question!

Josh

You can call dict.Remove(my_key). If the value of the dictionary is an object (as it seems to be in your case), that’ll not delete the object from the Realm, just from the dictionary. If you want to remove it both from the dictionary and the Realm, you can either do something like:

var val = dict[my_key];
realm.Remove(my_val);
dict.Remove(my_key);

or use embedded objects for the dictionary value.

Thanks for the prompt response, doing dict.Remove(my_key) worked fine. I am using embedded objects, and the embedded object and key were removed from the dictionary. I knew this answer was simple! Thanks again! Josh

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.