Question Compare 2 objects with different name but same properties

fearfulsc2

New member
Joined
Dec 10, 2018
Messages
2
Programming Experience
5-10
Hi everyone, I am working on a project for work and the technical design given to me wants me to be able to compare 2 different objects that have the same properties.

Okay, not so bad but then it gets more complex.

There is a third object that has its own set of properties and I need to compare the first two objects from this third object. I'll code a few example classes and explain in further detail.
C#:
public class HighSchoolStudent
{
 public string FirstName { get; set; }
 public string LastName { get; set; }
 public string Ssn {get; set; }
 public HighSchoolStudentAddress Address { get; set; }
}

public class MiddleSchoolStudent
{
 public string FirstName { get; set; }
 public string LastName { get; set; }
 public string Ssn {get; set; }
 public MiddleSchoolStudentAddress Address { get; set; }
}

public class DifferenceFields
{
 public string GroupCode { get; set; }
 public string FieldCode { get; set; }
 public string FieldName { get; set; }
 public bool Compare { get; set; }
}

public class DifferenceGroups
{
 public string GroupCode { get; set; }
 public string GroupName { get; set; }
}

This is a more basic example of what I am actually working with but I hope it will make my question clearer. Let's say the DifferencesFields class gets this set of data from the database
StudentFirstNameFirst Name1
StudentLastNameLast Name1
StudentSsnSocial Security Number1
StudentAddressStudent Address0

The goal is to Compare HighSchoolStudent and MiddleSchoolStudent by each of their properties to check for any mismatches/differences between the two objects based off the Fields data and whether we actually want to compare that data or not. The last row in the table shared shows that we want to ignore that comparison for Address while we want to check for all the others.

The HighSchoolStudent and MiddleSchoolStudent can both be different sizes or the same size.
I want to be able see which ones match and which ones are different. There's a more complex requirement wanting me to see if the Ssn(in this example at least) is equal to the other. For example, High School Student has Ssn 123456789
and Middle School Student has Ssn 123-45-6789. At first comparison, they are not equal to each other so they do not match, but we want to then do a second comparison to see if they match if we remove special characters. In this case, they would match. I would then give them a match rating of under 100% since it wasn't a perfect match from the start.

Does anyone have any suggestions on how to go about this and how I can best tackle this?

I was trying to do LINQ and use reflection but I am still running into problems.

Thank you!
 
This should provide a place to start:
bool?[] Compare(object obj1, object obj2, IEnumerable<DifferenceFields> fields)
{
    var results = new List<bool?>();

    var type1 = obj1.GetType();
    var type2 = obj2.GetType();

    foreach (var field in fields)
    {
        if (field.Compare)
        {
            var propertyName = field.FieldCode;
            var property1 = type1.GetProperty(propertyName);
            var property2 = type2.GetProperty(propertyName);

            results.Add(object.Equals(property1.GetValue(obj1), property2.GetValue(obj2)));
        }
        else
        {
            results.Add(null);
        }
    }

    return results.ToArray();
}
 
This is a basic example to compare 2 objects
[/URL]
Step 1 : Create a console application and add class Student with the properties as below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Compare2Objects
{
    public class Program
    {
        static void Main(string[] args)
        {
        }
    }
    public class Student
    {
        public string Name { get; set; }
        public int StudentId { get; set; }
        public int Age { get; set; }
    }
}

Step-2: Add objects for students as below in the Main method.
     Student Student1 = new Student() { Name = "Jack", Age = 15, StudentId = 100 };
     Student Student2 = new Student() { Name = "Smith", Age = 15, StudentId = 101 };
     Student Student3 = new Student() { Name = "Smith", Age = 15, StudentId = 101 };
     Student Student4 = new Student() { Name = "Smit", Age = 15, StudentId = 102 };
     Student Student5 = new Student() { Name = null, Age = 15, StudentId = 100 };

Step-3: Now I want to compare these above student objects with each other. I have added the following method inside the "Program" class.
     static bool Compare<T>(T Object1, T object2)
     {
          //Get the type of the object
          Type type = typeof(T);

          //return false if any of the object is false
          if (Object1 == null || object2 == null)
             return false;

         //Loop through each properties inside class and get values for the property from both the objects and compare
         foreach (System.Reflection.PropertyInfo property in type.GetProperties())
         {
              if (property.Name != "ExtensionData")
              {
                  string Object1Value = string.Empty;
                  string Object2Value = string.Empty;
                  if (type.GetProperty(property.Name).GetValue(Object1, null) != null)
                        Object1Value = type.GetProperty(property.Name).GetValue(Object1, null).ToString();
                  if (type.GetProperty(property.Name).GetValue(object2, null) != null)
                        Object2Value = type.GetProperty(property.Name).GetValue(object2, null).ToString();
                  if (Object1Value.Trim() != Object2Value.Trim())
                  {
                      return false;
                  }
              }
         }
         return true;
     }

The above method will get properties of the class and compare the values for each property. If any of the values are different, it will return false, else it will return true.

Step 4: So I am going to compare the objects by calling this method from the Main method as below.
     if (Compare<Student>(Student1, Student2))
           Console.WriteLine("Both objects are equal");
     else
          Console.WriteLine("Both objects are not equal");
     if (Compare<Student>(Student3, Student2))
          Console.WriteLine("Both objects are equal");
     else
          Console.WriteLine("Both objects are not equal");
     if (Compare<Student>(Student3, Student4))
          Console.WriteLine("Both objects are equal");
     else
          Console.WriteLine("Both objects are not equal");
     if (Compare<Student>(Student1, Student5))
          Console.WriteLine("Both objects are equal");
     else
          Console.WriteLine("Both objects are not equal");
          Console.Read();

The output will be:

Both objects are not equal
Both objects are equal
Both objects are not equal
Both objects are not equal
 
Back
Top Bottom