Lets say we have two collections as under
List<Person> persons1 = new List<Person>
{
new Person {Id = 1, Name = "Person 1"},
new Person {Id = 2, Name = "Person 2"},
new Person {Id = 3, Name = "Person 3"},
new Person {Id = 4, Name = "Person 4"}
};
List<Person> persons2 = new List<Person>
{
new Person {Id = 1, Name = "Person 1"},
new Person {Id = 2, Name = "Person 2"},
new Person {Id = 3, Name = "Person 3"},
new Person {Id = 4, Name = "Person 4"},
};
Objective is to compare the collection. Below is the program to do so
persons1.Where(x1 => !persons2.Any(x2 => x1.Id == x2.Id && x1.Name == x2.Name ))
.Union( persons2.Where(x1 => !persons1.Any(x2 => x1.Id == x2.Id && x1.Name == x2.Name )))
.Count() == 0 ? Console.WriteLine("Collections are equal") : Console.WriteLine("Collections are not equal");