stackoverflow.com/questions/67127955/c-sharp-how-do-you-remove-a-duplicate-object-in-a-list-where-the-object-itself

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using KellermanSoftware.CompareNetObjects;

public class Program
{
	public static Guid guid1 = Guid.NewGuid();	
	public static Guid guid2 = Guid.NewGuid();

	public static ComplexTypeObj cto1 = new ComplexTypeObj
	{
		Searches = new List<ComplexObject> { new ComplexObject { SomeQuery = "abcdefg", SomeProperty = "SomeValue" },
											new ComplexObject { SomeQuery = "abcd", SomeProperty = "SomeValueNewValue" },
											new ComplexObject { SomeQuery = "ag", SomeProperty = "SomeOtherValue"}
										   },
		Ids = new List<Guid> {guid1, guid2},
		Surnames = new List<string> {"Doe", "Rae", "Me"}
	};
	
	
	public static ComplexTypeObj cto2 = new ComplexTypeObj
	{
		Searches = new List<ComplexObject> { new ComplexObject { SomeQuery = "abcdefg", SomeProperty = "SomeValue" },
											new ComplexObject { SomeQuery = "abcd", SomeProperty = "SomeValueNewValue" },
											new ComplexObject { SomeQuery = "ag", SomeProperty = "SomeOtherValue"}
										   },
		Ids = new List<Guid> { guid1, guid2 },
		Surnames = new List<string> { "Doe", "Rae", "Me" }
	};
	
	public static ComplexTypeObj cto3 = new ComplexTypeObj
	{
		Searches = new List<ComplexObject> { new ComplexObject { SomeQuery = "abcdefg", SomeProperty = "SomeValue" },
											new ComplexObject { SomeQuery = "abcdefg", SomeProperty = "SomeValue" },
											new ComplexObject { SomeQuery = "abcdefg", SomeProperty = "SomeOtherValue"}
										   },
		Ids = new List<Guid> { guid1, guid2 },
		Surnames = new List<string> { "Doe", "Rae", "Me" }
	};

	public static ComplexTypeObj  cto4 = new ComplexTypeObj
	{
		Searches = new List<ComplexObject> { new ComplexObject { SomeQuery = "abcdefg", SomeProperty = "SomeValue" },
											new ComplexObject { SomeQuery = "abcdefg", SomeProperty = "SomeValue" },
											new ComplexObject { SomeQuery = "abcdefg", SomeProperty = "SomeOtherValue"}
										   },
		Ids = new List<Guid> { guid1, guid2 },
		Surnames = new List<string> { "Doe", "Rae", "Me" }
	};
	
	
	public static void Main()
	{
		// This is the comparison class
		var compareLogic = new CompareLogic();
		
		// Define some comparison configurations (More examples on the github page)
		compareLogic.Config.MaxDifferences = 100;
		compareLogic.Config.TreatStringEmptyAndNullTheSame = true;
		compareLogic.Config.CaseSensitive = true;

		// Compare the results
		var compareResults = compareLogic.Compare(cto3, cto1); // Where the magic happens.
		
		// Check if comparison is equal
		if(!compareResults.AreEqual)
		{
			// If ComparisonResults are not equal, write out
			// the differences in a single string
			compareResults.DifferencesString.Dump();
			
			// Or optionally iterate through the list of specific property differences between the two objects
			// foreach(var diff in compareResults.Differences)
			// {
		    //		diff.Dump();
			//}
		}
	}

	public class ComplexTypeObj
	{
		public List<ComplexObject> Searches = new List<ComplexObject>();
		public List<Guid> Ids = new List<Guid>();
		public List<string> Surnames = new List<string>();
	}

	public class ComplexObject
	{
		public string SomeQuery { get; set; }
		public string SomeProperty { get; set; }
	}
}
;