Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 689565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:20:44+00:00 2026-05-14T02:20:44+00:00

I have a method which compares two objects and returns a list of all

  • 0

I have a method which compares two objects and returns a list of all the property names which are different.

public static IList<string> GetDifferingProperties(object source, object target)
{
    var sourceType = source.GetType();
    var sourceProperties = sourceType.GetProperties();
    var targetType = target.GetType();
    var targetProperties = targetType.GetProperties();

    var properties = (from s in sourceProperties
                      from t in targetProperties
                      where s.Name == t.Name &&
                            s.PropertyType == t.PropertyType &&
                            s.GetValue(source,null) != t.GetValue(target,null)
                      select s.Name).ToList();
    return properties;
}

For example if I have two classes as follows:

public class Address
    {
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
    }

       public class Employee
        {
            public string FirstName { get; set; }
            public string MiddleName { get; set; }
            public string LastName { get; set; }
            public Address EmployeeAddress { get; set; }
        }

I am trying to compare the following two employee instances:

var emp1Address = new Address();
        emp1Address.AddressLine1 = "Microsoft Corporation";
        emp1Address.AddressLine2 = "One Microsoft Way";
        emp1Address.City = "Redmond";
        emp1Address.State = "WA";
        emp1Address.Zip = "98052-6399";

        var emp1 = new Employee();
        emp1.FirstName = "Bill";
        emp1.LastName = "Gates";
        emp1.EmployeeAddress = emp1Address;


        var emp2Address = new Address();
        emp2Address.AddressLine1 = "Gates Foundation";
        emp2Address.AddressLine2 = "One Microsoft Way";
        emp2Address.City = "Redmond";
        emp2Address.State = "WA";
        emp2Address.Zip = "98052-6399";

        var emp2 = new Employee();
        emp2.FirstName = "Melinda";
        emp2.LastName = "Gates";
        emp2.EmployeeAddress = emp2Address;

So when I pass these two employee objects to my GetDifferingProperties method currently it returns FirstName and EmployeeAddress, but it does not tell me which exact property (which in this case is Address1) in the EmployeeAddress has changed. How can I tweak this method to get something like EmployeeAddress.Address1?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:20:44+00:00Added an answer on May 14, 2026 at 2:20 am

    It’s because you are using != which, for objects, tests the identity of an object rather than its value. The key is to use recursion to generate the list of properties of properties. This will go as deep as you want…

    public static IList<string> GetDifferingProperties(object source, object target)
    {
      var sourceType = source.GetType();
      var sourceProperties = sourceType.GetProperties();
      var targetType = target.GetType();
      var targetProperties = targetType.GetProperties();
    
      var result = new List<string>();
    
      foreach (var property in
          (from s in sourceProperties
           from t in targetProperties
           where s.Name == t.Name &&
           s.PropertyType == t.PropertyType &&
           !Equals(s.GetValue(source, null), t.GetValue(target, null))
           select new { Source = s, Target = t }))
      {
        // it's up to you to decide how primitive is primitive enough
        if (IsPrimitive(property.Source.PropertyType))
        {
          result.Add(property.Source.Name);
        }
        else
        {
          foreach (var subProperty in GetDifferingProperties(
              property.Source.GetValue(source, null),
              property.Target.GetValue(target, null)))
          {
            result.Add(property.Source.Name + "." + subProperty);
          }
        }
      }
    
      return result;
    }
    
    private static bool IsPrimitive(Type type)
    {
      return type == typeof(string) || type == typeof(int);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# method which accepts a Predicate<Foo> and returns a list of
I have a method which never returns a null object. I want to make
I have a list with two or more objects of class Agent. Name =
I have a list of objects and Comparator which is used for sorting and
I have a method which takes params object[] such as: void Foo(params object[] items)
I have a method which should be executed in an exclusive fashion. Basically, it's
I have an method which save files to the internet, it works but just
I have a method which is given the parameter bool sortAscending. Now I want
I have a method which should be delayed from running for a specified amount
I have a method which constructs an object, calls an Execute method, and frees

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.