With custom classes, we can easily determine if two lists contain the same elements regardless of sequencing in their respective lists. Let's take a look.
Start with a custom class as shown below. Notice how I've used IEquatable to provide a custom implementation of Equals and GetHashCode()
- public class StudentModel : IEquatable<StudentModel>
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
-
- public override bool Equals(object obj)
- {
- return Equals(obj as StudentModel);
- }
-
- public bool Equals(StudentModel other)
- {
- return other != null &&
- FirstName == other.FirstName &&
- LastName == other.LastName;
- }
-
- public override int GetHashCode()
- {
- var hashCode = 1938039292;
- hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(FirstName);
- hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(LastName);
- return hashCode;
- }
-
- public static bool operator ==(StudentModel model1, StudentModel model2)
- {
- return EqualityComparer<StudentModel>.Default.Equals(model1, model2);
- }
-
- public static bool operator !=(StudentModel model1, StudentModel model2)
- {
- return !(model1 == model2);
- }
- }
To override the Equals, GetHashCode() and operators I used the Quick Actions and Refactoring functionality. It's available by right-clicking on the Class name, selecting Quick Actions and Refactoring and then selecting Generate Equals and GetHashCode(). It's a quick and consistent way for overriding these methods.
From here, let's build a custom class to contain our list using the following code.
- public class StudentModels : List<StudentModel>
- {
- public override bool Equals(object obj)
- {
- StudentModels other = (StudentModels)obj;
- return this.All(x => other.Contains(x)) && other.All(y => this.Contains(y));
- }
-
- }
Notice the override of the Equals method. In that method, the real magic occurs.
A straight comparison of List1.Equals(List2) will not work. You have to use the .Contains() method in LINQ. Also, take note of the dual comparison from (this.All(x => other.Contains(x)) followed by other.All(y => this.Contains(y)). Why?
If you don't do them both, you aren't going to receive the answer to the question, i.e., does every element in List1 has an equal element in List2 and vice versa. You will only receive an answer to whether or not one list is a subset of the other.
Now, let's take a look at the code to perform the comparison.
- Framework.Students.StudentModel student1 = new Framework.Students.StudentModel()
- {
- FirstName = "FirstName1",
- };
-
- Framework.Students.StudentModel student2 = new Framework.Students.StudentModel()
- {
- FirstName = "FirstName1"
- };
-
- Framework.Students.StudentModels listA = new Framework.Students.StudentModels();
- Framework.Students.StudentModels listB = new Framework.Students.StudentModels();
-
- listA.Add(student1);
- listB.Add(student2);
-
- bool all = listA.Equals(listB);
In this case, both lists will be equal. We've spun up two StudentModel objects with the same first name and added them to two different lists. From there, .Equals() determines if both lists have the same elements.
If you have the following code, the Equals override in StudentModels will return false because we are performing the two passes. Student3 is an element of listB, but not a member of listA.
- Framework.Students.StudentModel student1 = new Framework.Students.StudentModel()
- {
- FirstName = "FirstName1",
- };
-
- Framework.Students.StudentModel student2 = new Framework.Students.StudentModel()
- {
- FirstName = "FirstName1"
- };
-
- Framework.Students.StudentModel student3 = new Framework.Students.StudentModel()
- {
- FirstName = "FirstName3"
- };
-
- Framework.Students.StudentModels listA = new Framework.Students.StudentModels();
- Framework.Students.StudentModels listB = new Framework.Students.StudentModels();
-
- listA.Add(student1);
- listB.Add(student2);
- listB.Add(student3);
-
- bool all = listA.Equals(listB);
That's it. You now have a true representation that the two lists are equal.
One thing to point out is that this method does not perform an element by element comparision. It will not tell you if ListA[0] = ListB[0] and ListA[1] = ListB[1] and so on. It does tell you if ListA[0] is contained in ListB. For a sequenced element comparison, you can use SequenceEqual.
- bool areSequencesEqual = listA.SequenceEqual(listB);
That's it! Happy coding!