Search code examples
c#expandoobject

Removing a property in ExpandoObject


I am using an API to get a collection of JSON responses and I am putting them in an ExpandoObject result. The property I want to delete is in result.data.attributes.image_url for example. I don't need it and wish to remove it from the entire collection. So far I've tried:

result.data.attributes.Remove("image_url"); 

However I am getting the message that ExpandoObject does not have a "Remove" method. How exactly can I do this? In python I would just use the del keyword to delete a dataframe's column. What is a C# equivalent to this?


Solution

  • First cast to a dictionary, then you should be able to call Remove().

    var dict = (IDictionary<string, object>) result.data.attributes;
    dict.Remove("image_url");
    

    Or an all-in-one-line version, IMO harder to read though:

    ((IDictionary<string, object>) result.data.attributes).Remove("image_url");