C#C
C#2y ago
SWEETPONY

✅ how to merge two objects?

Sometimes ago I had this object:
public sealed record CreateFlightLegModel
{
    public string? Identity { get; init; }
    public LegIdentityArgument? LegIdentity { get; init; }
    public JsonDocument? LegData { get; set; }
}


LegData used in method for merging and for example we can do this:
var firstLeg =
    JsonDocument.Parse(@"{
    ""fuel"":
    {
      ""fuel_invoice_1"": ""1"",
      ""fuel_invoice_2"": ""2"",
      ""fuel_invoice_3"": ""3""
    }
}");


var secondLeg=
    JsonDocument.Parse(@"{
    ""fuel"":
    {
      ""fuel_invoice_2"": null,
      ""fuel_invoice_3"": null
    }
}");


and the output will be:
{
    ""fuel"":
    {
      ""fuel_invoice_1"": ""1"",
      ""fuel_invoice_2"": null,
      ""fuel_invoice_3"": null
    }
}


then we decided move from JsonDocument to model and this merge doesn't work anymore
in case I pasted earlier output will be:
""fuel"":
    {
      ""fuel_invoice_1"": null,
      ""fuel_invoice_2"": null,
      ""fuel_invoice_3"": null
    }


can someone understand me what to do?
Was this page helpful?