Object Comparison In C# - Part Two [N Level Down List Comparison]

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.

  1. you can compare two objects.
  2. 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.

  1. public class People {  
  2.     public int PeopleId {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string PeopleName {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public int PeopleAge {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public DateTime PeopleDOB {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public List < Department > PeopleDepartment {  
  19.         get;  
  20.         set;  
  21.     }  

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.

  1. public class Department {  
  2.     public int DeptId {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string DeptName {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public List < Block > DepBlock {  
  11.         get;  
  12.         set;  
  13.     }  
  14. }  
  15. public class Block {  
  16.     public int BlockId {  
  17.         get;  
  18.         set;  
  19.     }  
  20.     public string BlockName {  
  21.         get;  
  22.         set;  
  23.     }  

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.

  1. public static class ObjectCompare {  
  2.     public static bool CompareList(dynamic object1, dynamic object2) {  
  3.         bool match = false;  
  4.         List < dynamic > e1 = new List < dynamic > ();  
  5.         e1.AddRange(object1);  
  6.         List < dynamic > e2 = new List < dynamic > ();  
  7.         e2.AddRange(object2);  
  8.         int countFirst, countSecond;  
  9.         countFirst = e1.Count;  
  10.         countSecond = e2.Count;  
  11.         if (countFirst == countSecond) {  
  12.             countFirst = e1.Count - 1;  
  13.             while (countFirst > -1) {  
  14.                 match = false;  
  15.                 countSecond = e2.Count - 1;  
  16.                 while (countSecond > -1) {  
  17.                     match = Compare(e1[countFirst], e2[countSecond]);  
  18.                     if (match) {  
  19.                         e2.Remove(e2[countSecond]);  
  20.                         countSecond = -1;  
  21.                     }  
  22.                     if (match == false && countSecond == 0) {  
  23.                         return false;  
  24.                     }  
  25.                     countSecond--;  
  26.                 }  
  27.                 countFirst--;  
  28.             }  
  29.         } else {  
  30.             return false;  
  31.         }  
  32.         return match;  
  33.     }  
  34.     public static bool Compare(object e1, object e2) {  
  35.         bool flag = true;  
  36.         foreach(PropertyInfo propObj1 in e1.GetType().GetProperties()) {  
  37.             var propObj2 = e2.GetType().GetProperty(propObj1.Name);  
  38.             if (propObj1.PropertyType.Name.Equals("List`1")) {  
  39.                 List < dynamic > objList1 = new List < object > () {  
  40.                     propObj1.GetValue(e1, null)  
  41.                 };  
  42.                 List < dynamic > objList2 = new List < object > () {  
  43.                     propObj2.GetValue(e2, null)  
  44.                 };  
  45.                 if (!CompareList(objList1[0], objList2[0])) {  
  46.                     return false;  
  47.                 }  
  48.             } else if (!(propObj1.GetValue(e1, null).Equals(propObj2.GetValue(e2, null)))) {  
  49.                 flag = false;  
  50.                 return flag;  
  51.             }  
  52.         }  
  53.         return flag;  
  54.     }  
  55. }   

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.

  1. static void Main(string[] args) {  
  2.     bool compare;  
  3.     var datetime = DateTime.Now;  
  4.     People p1 = new People {  
  5.         PeopleId = 1, PeopleAge = 25, PeopleName = "Sam", PeopleDOB = datetime, PeopleDepartment = new List < Department > {  
  6.             new Department() {  
  7.                 DeptId = 1, DeptName = "android", DepBlock = new List < Block > {  
  8.                     new Block() {  
  9.                         BlockId = 1, BlockName = "A"  
  10.                     },  
  11.                     new Block() {  
  12.                         BlockId = 2, BlockName = "B"  
  13.                     }  
  14.                 }  
  15.             },  
  16.             new Department() {  
  17.                 DeptId = 2, DeptName = "ios", DepBlock = new List < Block > {  
  18.                     new Block() {  
  19.                         BlockId = 3, BlockName = "C"  
  20.                     },  
  21.                     new Block() {  
  22.                         BlockId = 4, BlockName = "D"  
  23.                     }  
  24.                 }  
  25.             }  
  26.         }  
  27.     };  
  28.     People p2 = new People {  
  29.         PeopleId = 2, PeopleAge = 26, PeopleName = "Tom", PeopleDOB = datetime, PeopleDepartment = new List < Department > {  
  30.             new Department() {  
  31.                 DeptId = 3, DeptName = "mvc", DepBlock = new List < Block > {  
  32.                     new Block() {  
  33.                         BlockId = 1, BlockName = "A"  
  34.                     },  
  35.                     new Block() {  
  36.                         BlockId = 2, BlockName = "B"  
  37.                     }  
  38.                 }  
  39.             },  
  40.             new Department {  
  41.                 DeptId = 4, DeptName = "mvvm", DepBlock = new List < Block > {  
  42.                     new Block() {  
  43.                         BlockId = 1, BlockName = "A"  
  44.                     },  
  45.                     new Block() {  
  46.                         BlockId = 2, BlockName = "B"  
  47.                     }  
  48.                 }  
  49.             }  
  50.         }  
  51.     };  
  52.     People p3 = new People {  
  53.         PeopleId = 1, PeopleAge = 25, PeopleName = "Sam", PeopleDOB = datetime, PeopleDepartment = new List < Department > {  
  54.             new Department() {  
  55.                 DeptId = 1, DeptName = "android", DepBlock = new List < Block > {  
  56.                     new Block() {  
  57.                         BlockId = 1, BlockName = "A"  
  58.                     },  
  59.                     new Block() {  
  60.                         BlockId = 2, BlockName = "B"  
  61.                     }  
  62.                 }  
  63.             },  
  64.             new Department() {  
  65.                 DeptId = 2, DeptName = "ios", DepBlock = new List < Block > {  
  66.                     new Block() {  
  67.                         BlockId = 3, BlockName = "C"  
  68.                     },  
  69.                     new Block() {  
  70.                         BlockId = 4, BlockName = "D"  
  71.                     }  
  72.                 }  
  73.             }  
  74.         }  
  75.     };  
  76.     // List of objects to compare  
  77.     List < People > PeopleList1 = new List < People > () {  
  78.         p1,  
  79.         p3  
  80.     };  
  81.     List < People > PeopleList2 = new List < People > () {  
  82.         p1,  
  83.         p2  
  84.     };  
  85.     List < People > PeopleList3 = new List < People > () {  
  86.         p1,  
  87.         p3  
  88.     };  
  89.     Console.WriteLine("List of objects comparision:");  
  90.     compare = ObjectCompare.CompareList(PeopleList1, PeopleList2);  
  91.     if (compare) {  
  92.         Console.WriteLine("PeopleList1 and PeopleList2 Objects are Equal");  
  93.     } else {  
  94.         Console.WriteLine("PeopleList1 and PeopleList2 Objects are Not equal");  
  95.     }  
  96.     compare = ObjectCompare.CompareList(PeopleList1, PeopleList3);  
  97.     if (compare) {  
  98.         Console.WriteLine("PeopleList1 and PeopleList3 Objects are Equal");  
  99.     } else {  
  100.         Console.WriteLine("PeopleList1 and PeopleList3 Objects are Not equal");  
  101.     }  
  102.     compare = ObjectCompare.CompareList(PeopleList2, PeopleList3);  
  103.     if (compare) {  
  104.         Console.WriteLine("PeopleList2 and PeopleList3 Objects are Equal");  
  105.     } else {  
  106.         Console.WriteLine("PeopleList2 and PeopleList3 Objects are Not equal");  
  107.     }  
  108.     //Two objects comparision  
  109.     Console.WriteLine("\n \nTwo objects comparision:");  
  110.     compare = ObjectCompare.Compare(p1, p2);  
  111.     if (compare) {  
  112.         Console.WriteLine("p1 and p2 People Objects are Equal");  
  113.     } else {  
  114.         Console.WriteLine("p1 and p2 Peoples objects are Not equal");  
  115.     }  
  116.     compare = ObjectCompare.Compare(p1, p3);  
  117.     if (compare) {  
  118.         Console.WriteLine("p1 and p3 People Objects are Equal");  
  119.     } else {  
  120.         Console.WriteLine("p1 and p3 Peoples objects are Not equal");  
  121.     }  
  122.     compare = ObjectCompare.Compare(p2, p3);  
  123.     if (compare) {  
  124.         Console.WriteLine("p2 and p3 People Objects are Equal");  
  125.     } else {  
  126.         Console.WriteLine("p2 and p3 Peoples objects are Not equal");  
  127.     }  
  128. }  

Below is the output screen for the given application.

output