Home > other >  ExpandoObject changing the value
ExpandoObject changing the value

Time:08-09

I have an ExpandoObject. inside the object there are bool values. i need to change that to a string. here is an example code. In the dict the value of coke needs to be changed to string "true" and Coke "false". I cannot search for the key like coke or pepsi. This is just an example. I don't know what the key will be. if the value is false or true i need to change it to a string.

 Dictionary<string, object> myDictionary = new Dictionary<string, object>();
        myDictionary.Add("FName", "John");
        myDictionary.Add("LName2", "Walter");
        myDictionary.Add("Coke", true);
        myDictionary.Add("Pepsi", false);

        var merge_fields = new ExpandoObject() as IDictionary<string, Object>;

        foreach (var property in myDictionary)
        {

                merge_fields.Add(property.Key, property.Value);

        }

CodePudding user response:

Write a conversion function:

object Converter(object input)
{
    if (input is bool)
    {
        return ((bool)input) ? "true" : "false";
    }
    else
    {
        return input;
    }
}

Then use it in your loop:

foreach (var property in myDictionary)
{
    merge_fields.Add(property.Key, Converter(property.Value));
}
  • Related