"c# compare two objects for changes" Code Answer's
You're definitely familiar with the best coding language TypeScript that developers use to develop their projects and they get all their queries like "c# compare two objects for changes" answered properly. Developers are finding an appropriate answer about c# compare two objects for changes related to the TypeScript coding language. By visiting this online portal developers get answers concerning TypeScript codes question like c# compare two objects for changes. Enter your desired code related query in the search bar and get every piece of information about TypeScript code related question on c# compare two objects for changes.
c# compare two objects for changes
public static JObject FindDiff(this JToken Current, JToken Model)
{
var diff = new JObject();
if (JToken.DeepEquals(Current, Model)) return diff;
switch(Current.Type)
{
case JTokenType.Object:
{
var current = Current as JObject;
var model = Model as JObject;
var addedKeys = current.Properties().Select(c => c.Name).Except(model.Properties().Select(c => c.Name));
var removedKeys = model.Properties().Select(c => c.Name).Except(current.Properties().Select(c => c.Name));
var unchangedKeys = current.Properties().Where(c => JToken.DeepEquals(c.Value, Model[c.Name])).Select(c => c.Name);
foreach (var k in addedKeys)
{
diff[k] = new JObject
{
["+"] = Current[k]
};
}
foreach (var k in removedKeys)
{
diff[k] = new JObject
{
["-"] = Model[k]
};
}
var potentiallyModifiedKeys = current.Properties().Select(c => c.Name).Except(addedKeys).Except(unchangedKeys);
foreach (var k in potentiallyModifiedKeys)
{
var foundDiff = FindDiff(current[k], model[k]);
if(foundDiff.HasValues) diff[k] = foundDiff;
}
}
break;
case JTokenType.Array:
{
var current = Current as JArray;
var model = Model as JArray;
var plus = new JArray(current.Except(model, new JTokenEqualityComparer()));
var minus = new JArray(model.Except(current, new JTokenEqualityComparer()));
if (plus.HasValues) diff["+"] = plus;
if (minus.HasValues) diff["-"] = minus;
}
break;
default:
diff["+"] = Current;
diff["-"] = Model;
break;
}
return diff;
}
Source: stackoverflow.com
All those coders who are working on the TypeScript based application and are stuck on c# compare two objects for changes can get a collection of related answers to their query. Programmers need to enter their query on c# compare two objects for changes related to TypeScript code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about c# compare two objects for changes for the programmers working on TypeScript code while coding their module. Coders are also allowed to rectify already present answers of c# compare two objects for changes while working on the TypeScript language code. Developers can add up suggestions if they deem fit any other answer relating to "c# compare two objects for changes". Visit this developer's friendly online web community, CodeProZone, and get your queries like c# compare two objects for changes resolved professionally and stay updated to the latest TypeScript updates.