This may be overkill for your purposes, but my favorite library for comparing complex objects is CompareNETObjects
(GitHub: https://github.com/GregFinzer/Compare-Net-Objects)
(NuGet: Install-Package CompareNETObjects -Version 4.73.0)
Here is a .NET Fiddle with a working example: https://dotnetfiddle.net/vP2ya3
// This is the comparison class
var compareLogic = new CompareLogic();
// (Optionally define some configurations for the CompareLogic)
// e.g. Compare different types, Ignore props, Case sensitivity, etc.
compareLogic.Config.IgnoreProperty<ComplexTypeObj>(x => x.Searches);
compareLogic.Config.TreatStringEmptyAndNullTheSame = true;
compareLogic.Config.CaseSensitive = true;
// Compare the results
var compareResults = compareLogic.Compare(cto3, cto1); // Where the magic happens.
// Check if objects are equal
if(!compareResults.AreEqual)
{
// Do something...
}
But in addition to just checking if they are equal, the ComparisonResult
object returned will let you view individual differences between object properties or get a string of all property differences just as a few examples.