First we pull in new alerts and deserialize them. Now I only care about 2 properties that need to be compared: CommandID and AlertID, all others can be ignored so I create a new object which I assumed would have been easier to compare the results. All other properties become null.
List<AlertModel> alerts = JsonConvert.DeserializeObject<List<AlertModel>>(json)
.Select(x => new AlertModel() { CommandID = x.CommandID, AlertID = x.AlertID }).ToList();
Now I want to find new alerts that don't already exist
List<AlertModel> newAlerts = alerts.Except(currentAlerts).ToList();
Next we pull what alerts already exist.
List<AlertModel> existingAlerts = currentAlerts.Intersect(alerts).ToList();
Now we store new and existing alerts.
currentAlerts.Clear();
currentAlerts.AddRange(newAlerts);
currentAlerts.AddRange(existingAlerts);
1st run alerts
contains 1 item newAlerts
contains 1 item and existingAlerts
contains 0 as they should.
2nd run through isn't what I was expecting.
alerts
contains 1 as it should.
newAlerts
contains 1 and this should be 0. currentAlerts contains the exact same CommandID and AlertID as in alerts
existingAlerts
contains 0 and this should be 1 since the same CommandID and AlertID exists in currentAlerts
and alerts
.
Not sure what i'm missing here and maybe there is a better way to do this.