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 7043299
In Process

The Archive Base Latest Questions

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

I am working on a method that compares two objects using reflection. The object

  • 0

I am working on a method that compares two objects using reflection. The object types are objects created from entity framework. When I use GetProperties() I am getting EntityCollection and EntityReference properties. I only want the properties that belong to the object and not any associated properties or references from foreign keys.

I’ve tried the following How to get all names of properties in an Entity?.

I thought about passing an array of properties to compare but I don’t want to have to type them in for each object type. I am open to some suggestions even those that don’t use reflection.

public bool CompareEntities<T>(T oldEntity, T newEntity)
{
    bool same = true;
    PropertyInfo[] properties = oldEntity.GetType().GetProperties();

    foreach (PropertyInfo property in properties)
    {
        var oldValue = property.GetValue(oldEntity, null);
        var newValue = property.GetValue(newEntity, null);

        if (oldValue != null && newValue != null)
        {
            if (!oldValue.Equals(newValue))
            {
                same = false;
                break;
            }
        }
        else if ((oldValue == null && newValue != null) || (oldValue != null && newValue == null))
        {
            same = false;
            break;
        }
    }
    return same;
}
  • 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-28T02:16:21+00:00Added an answer on May 28, 2026 at 2:16 am

    Using the suggestions from @Eranga and https://stackoverflow.com/a/5381986/1129035 I was able to come up with a workable solution.

    Since some of the properties in the root object are a GenericType there needs to be two different if statements. Only if the current property is an EntityCollection skip over it.

    public bool CompareEntities<T>(T oldEntity, T newEntity)
    {
        bool same = true;
        PropertyInfo[] properties = oldEntity.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
            .Where(pi => !(pi.PropertyType.IsSubclassOf(typeof(EntityObject)))
            && !(pi.PropertyType.IsSubclassOf(typeof(EntityReference)))
            ).ToArray();
    
        foreach (PropertyInfo property in properties)
        {
            if (property.PropertyType.IsGenericType)
            {
                if (property.PropertyType.GetGenericTypeDefinition() == typeof(EntityCollection<>))
                {
                    continue;
                }
            }
    
            var oldValue = property.GetValue(oldEntity, null);
            var newValue = property.GetValue(newEntity, null);
    
            if (oldValue != null && newValue != null)
            {
                if (!oldValue.Equals(newValue))
                {
                    same = false;
                    break;
                }
            }
            else if ((oldValue == null && newValue != null) || (oldValue != null && newValue == null))
            {
                same = false;
                break;
            }
        }
    
        return same;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an extension method that's only applicable to reference types. I think,
I'm working on a method that accepts an expression tree as a parameter, along
I am working on a method that will allow me to pull in the
I was working on some code recently and came across a method that had
I am working with ASP.net. I am trying to call a method that exists
I have mocks working where I test that methods on my mocked object are
I'm working on a JSP where I need to call methods on object that
I have a method that takes an object as an argument. Both the caller
I am working on a program (learning C#) that creates a object (Card) and
I have two collections of my own reference-type objects that I wrote my own

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.