Introduction
In my previous article, the object comparison was made capable only for 1 level down list object. This article will demonstrate the comparison of the objects (or list of objects) with n level down list comparison. You can have any class that contains the list property, that can contain the further list properties, and so on.
We are going to compare two objects of the same class having collection property as List or multiple collection properties with N level.
Comparision can be done on the following basis.
- you can compare two objects.
- You can compare two lists of objects.
Note
Objects can contain the list properties. These lists can contain further list properties and so on. Our main aim is to compare the objects with multiple levels of list properties.
Implementation
Create a console application (say CompareObject) in C# using Visual Studio and add classes as below. You can create these as a separate file also.
- public class People {
- public int PeopleId {
- get;
- set;
- }
- public string PeopleName {
- get;
- set;
- }
- public int PeopleAge {
- get;
- set;
- }
- public DateTime PeopleDOB {
- get;
- set;
- }
- public List < Department > PeopleDepartment {
- get;
- set;
- }
- }
In the above class, the List<Department> property is defined that leads to further list property in term of List<Block>. So, the situation can be - this Block can also have list properties and so on.
- public class Department {
- public int DeptId {
- get;
- set;
- }
- public string DeptName {
- get;
- set;
- }
- public List < Block > DepBlock {
- get;
- set;
- }
- }
- public class Block {
- public int BlockId {
- get;
- set;
- }
- public string BlockName {
- get;
- set;
- }
- }
Add the static class ObjectCompare.cs
This class contains two static methods ( CompareList and Compare ). The first method is responsible for comparing the two List Objects and the second will compare the two given objects of the same class.
- public static class ObjectCompare {
- public static bool CompareList(dynamic object1, dynamic object2) {
- bool match = false;
- List < dynamic > e1 = new List < dynamic > ();
- e1.AddRange(object1);
- List < dynamic > e2 = new List < dynamic > ();
- e2.AddRange(object2);
- int countFirst, countSecond;
- countFirst = e1.Count;
- countSecond = e2.Count;
- if (countFirst == countSecond) {
- countFirst = e1.Count - 1;
- while (countFirst > -1) {
- match = false;
- countSecond = e2.Count - 1;
- while (countSecond > -1) {
- match = Compare(e1[countFirst], e2[countSecond]);
- if (match) {
- e2.Remove(e2[countSecond]);
- countSecond = -1;
- }
- if (match == false && countSecond == 0) {
- return false;
- }
- countSecond--;
- }
- countFirst--;
- }
- } else {
- return false;
- }
- return match;
- }
- public static bool Compare(object e1, object e2) {
- bool flag = true;
- foreach(PropertyInfo propObj1 in e1.GetType().GetProperties()) {
- var propObj2 = e2.GetType().GetProperty(propObj1.Name);
- if (propObj1.PropertyType.Name.Equals("List`1")) {
- List < dynamic > objList1 = new List < object > () {
- propObj1.GetValue(e1, null)
- };
- List < dynamic > objList2 = new List < object > () {
- propObj2.GetValue(e2, null)
- };
- if (!CompareList(objList1[0], objList2[0])) {
- return false;
- }
- } else if (!(propObj1.GetValue(e1, null).Equals(propObj2.GetValue(e2, null)))) {
- flag = false;
- return flag;
- }
- }
- return flag;
- }
- }
Modify the Main method of class/file Program.cs
In the below demonstration, we have created the People object and created the list of people objects. We have done comparison on the List of objects and also the comparison on individual objects.
- static void Main(string[] args) {
- bool compare;
- var datetime = DateTime.Now;
- People p1 = new People {
- PeopleId = 1, PeopleAge = 25, PeopleName = "Sam", PeopleDOB = datetime, PeopleDepartment = new List < Department > {
- new Department() {
- DeptId = 1, DeptName = "android", DepBlock = new List < Block > {
- new Block() {
- BlockId = 1, BlockName = "A"
- },
- new Block() {
- BlockId = 2, BlockName = "B"
- }
- }
- },
- new Department() {
- DeptId = 2, DeptName = "ios", DepBlock = new List < Block > {
- new Block() {
- BlockId = 3, BlockName = "C"
- },
- new Block() {
- BlockId = 4, BlockName = "D"
- }
- }
- }
- }
- };
- People p2 = new People {
- PeopleId = 2, PeopleAge = 26, PeopleName = "Tom", PeopleDOB = datetime, PeopleDepartment = new List < Department > {
- new Department() {
- DeptId = 3, DeptName = "mvc", DepBlock = new List < Block > {
- new Block() {
- BlockId = 1, BlockName = "A"
- },
- new Block() {
- BlockId = 2, BlockName = "B"
- }
- }
- },
- new Department {
- DeptId = 4, DeptName = "mvvm", DepBlock = new List < Block > {
- new Block() {
- BlockId = 1, BlockName = "A"
- },
- new Block() {
- BlockId = 2, BlockName = "B"
- }
- }
- }
- }
- };
- People p3 = new People {
- PeopleId = 1, PeopleAge = 25, PeopleName = "Sam", PeopleDOB = datetime, PeopleDepartment = new List < Department > {
- new Department() {
- DeptId = 1, DeptName = "android", DepBlock = new List < Block > {
- new Block() {
- BlockId = 1, BlockName = "A"
- },
- new Block() {
- BlockId = 2, BlockName = "B"
- }
- }
- },
- new Department() {
- DeptId = 2, DeptName = "ios", DepBlock = new List < Block > {
- new Block() {
- BlockId = 3, BlockName = "C"
- },
- new Block() {
- BlockId = 4, BlockName = "D"
- }
- }
- }
- }
- };
-
- List < People > PeopleList1 = new List < People > () {
- p1,
- p3
- };
- List < People > PeopleList2 = new List < People > () {
- p1,
- p2
- };
- List < People > PeopleList3 = new List < People > () {
- p1,
- p3
- };
- Console.WriteLine("List of objects comparision:");
- compare = ObjectCompare.CompareList(PeopleList1, PeopleList2);
- if (compare) {
- Console.WriteLine("PeopleList1 and PeopleList2 Objects are Equal");
- } else {
- Console.WriteLine("PeopleList1 and PeopleList2 Objects are Not equal");
- }
- compare = ObjectCompare.CompareList(PeopleList1, PeopleList3);
- if (compare) {
- Console.WriteLine("PeopleList1 and PeopleList3 Objects are Equal");
- } else {
- Console.WriteLine("PeopleList1 and PeopleList3 Objects are Not equal");
- }
- compare = ObjectCompare.CompareList(PeopleList2, PeopleList3);
- if (compare) {
- Console.WriteLine("PeopleList2 and PeopleList3 Objects are Equal");
- } else {
- Console.WriteLine("PeopleList2 and PeopleList3 Objects are Not equal");
- }
-
- Console.WriteLine("\n \nTwo objects comparision:");
- compare = ObjectCompare.Compare(p1, p2);
- if (compare) {
- Console.WriteLine("p1 and p2 People Objects are Equal");
- } else {
- Console.WriteLine("p1 and p2 Peoples objects are Not equal");
- }
- compare = ObjectCompare.Compare(p1, p3);
- if (compare) {
- Console.WriteLine("p1 and p3 People Objects are Equal");
- } else {
- Console.WriteLine("p1 and p3 Peoples objects are Not equal");
- }
- compare = ObjectCompare.Compare(p2, p3);
- if (compare) {
- Console.WriteLine("p2 and p3 People Objects are Equal");
- } else {
- Console.WriteLine("p2 and p3 Peoples objects are Not equal");
- }
- }
Below is the output screen for the given application.