0

I am using two Document queries that together fills all properties in a C# object.

They are returned as JObjects from the DB so I am using JObject.ToObject() in order to map the JSON to the C# object.

The problem is that this works great for the first query but when I do it for the second query, it overwrites and creates a new object instead of merging with the existing one.

I Understand this is desired behavior but how can I merge the second ToObject() call so that it does not overwrite the existing properties ?

Matt Douhan
  • 2,053
  • 1
  • 21
  • 40
  • How would you expect the merge to work? Which properties should be overwritten and which shouldn't? – Johnathan Barclay Apr 20 '20 at 14:22
  • @JohnathanBarclay They are not overlapping at all, DB query 1 has half of the properties and DB query 2 has the other half so there is no overlap with regards to properties – Matt Douhan Apr 20 '20 at 14:23
  • In that case you can [populate an object](https://www.newtonsoft.com/json/help/html/PopulateObject.htm). – Johnathan Barclay Apr 20 '20 at 14:26
  • Looks like a duplicate of [How can I populate an existing object from a JToken (using Newtonsoft.Json)?](https://stackoverflow.com/q/30220328/3744182), agree? Or do you have some additional requirement to control exactly which properties get overwritten? – dbc Apr 20 '20 at 14:43
  • @dbc looks like it yes – Matt Douhan Apr 20 '20 at 14:49

0 Answers0