3

How to create a dynamic object of the below representation in c#. Guid is passed in as a parameter

{$addToSet : {childFolders: "Guid"}}

I tried this :

dynamic expando = new ExpandoObject();
expando.$addSet = new ExpandoObject() as dynamic; // This line gives me error on visual studio . Unexpected character '$'
StackOverflowVeryHelpful
  • 2,347
  • 8
  • 34
  • 46

1 Answers1

2

With ExpandoObject Class

dynamic expando = new ExpandoObject();

((IDictionary<string, object>)expando).Add("$addSet", new ExpandoObject());

var addSet = ((IDictionary<string, object>)expando)["$addSet"] as dynamic;

addSet.childFolders = "Guid";

With Json.Net

dynamic obj = new JObject();
obj["$addSet"] = new JObject();
var addSet = obj["$addSet"];
addSet.childFolders = "Guid";
Jim
  • 2,974
  • 2
  • 19
  • 29