Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created January 29, 2024 01:17
Show Gist options
  • Save karenpayneoregon/86fc6be7a881d005c2f659f2a390f48d to your computer and use it in GitHub Desktop.
Save karenpayneoregon/86fc6be7a881d005c2f659f2a390f48d to your computer and use it in GitHub Desktop.
Basic example for CompareNETObjects NuGet package

Basic code to try out CompareNETObjects NuGet package for comparing objects.

Note 1

The following line excludes Human.Id and Country.Id from the compare operation

compare.Config.MembersToIgnore.Add("*Id");

Note 2

System.Text.Json is used to serialize bad results.

public class Country
{
public int Id { get; set; }
public string CountryName { get; set; }
}
public class Human
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateOnly BirthDay { get; set; }
public Country Country { get; set; }
}
List<Country> countries = [ new() { Id = 1, CountryName = "Mexico" }, new() { Id = 2, CountryName = "mexico" } ];
Human human1 = new() { Id = 1, FirstName = "Karen", LastName = "Payne", BirthDay = new DateOnly(1967,11,1), Country = countries[0] };
Human human2 = new() { Id = 2, FirstName = "Karen", LastName = "Payne", BirthDay = new DateOnly(1968, 11, 1), Country = countries[1] };
CompareLogic compare = new();
compare.Config.MembersToIgnore.Add("*Id");
ComparisonResult result = compare.Compare(human1, human2);
if (result.AreEqual)
{
Debug.WriteLine("Equal");
}
else
{
var json = JsonSerializer.Serialize(result.Differences, new JsonSerializerOptions { WriteIndented = true });
Debug.WriteLine(json);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment