C#C
C#4y ago
malkav

Can't get items to be added to update

I can't seem to get the "update" List filled..
I have this Json data https://hastebin.com/bazadowese.json
What I am trying to do, is everything that is not in the targetJson should be added to the "inject" list, and the items that are in the targetJson should be checked against the items in the sourceJson, if they are not in there, done. If they are check if they have differences, if they have differences add that item to the "update" list.

Currently it just adds everything from the sourceJson into the "inject" list and doesn't do squat for the update list.

Here's the function:
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
LookupInputs data = JsonConvert.DeserializeObject<LookupInputs>(requestBody);
if (data == null)
    return new BadRequestObjectResult("There was no data provided, or the data was not formatted properly");

Dictionary<string, List<Dictionary<string, JToken>>> result = new()
{
    { "inject", new() },
    { "update", new() }
};

if (data.TargetJson.Count == 0)
{
    result["inject"] = (from item in data.SourceJson select item).ToList();
}
else
{
    result["inject"] = (from source in data.SourceJson
        from target in data.TargetJson
        where source[data.CommonIdentifier] != target[data.CommonIdentifier] && !source.DictEquals(target)
        select source).ToList();

    result["update"] = (from source in data.SourceJson
        from target in data.TargetJson
        where source[data.CommonIdentifier] == target[data.CommonIdentifier] // && !source.DictEquals(target)
        select target).ToList();
}

return new OkObjectResult(results);

this is the current result: https://hastebin.com/buxaroneda.json
the result I'm expecting is that the item "BookID": "6" is left out of the "inject" list, and added to the "update" list
Was this page helpful?