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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:21:49+00:00 2026-06-07T04:21:49+00:00

I am comparing two objects of the same type and returning the differences into

  • 0

I am comparing two objects of the same type and returning the differences into a list of FieldChange objects. Right now I am listing out each field comparison like this which seems a bit suboptimal.

Is there a cleaner way of refactoring the code below to avoid the repetition? There are two sets of code below but in reality I have about 20 comparisons.

var changes = new List<FieldChange>();
if (proposedUpdatedProject.StatusId != existingProject.StatusId)
{
    var previousStatusName = existingProject.StatusShortName;
    existingProject.Status = ProjectModel.Repository.Fetch<ProjectStatus>(proposedUpdatedProject.StatusId);
    changes.Add(new FieldChange { FieldName = "Status", PreviousValue = previousStatusName, NewValue = existingProject.StatusShortName });
}

if (proposedUpdatedProject.TechOwnerId != existingProject.TechOwnerId)
{
    var previousTechOwnerName = existingProject.TechOwnerName;
    existingProject.TechOwner = ProjectModel.Repository.Fetch<Person>(proposedUpdatedProject.TechOwnerId);
    changes.Add(new FieldChange { FieldName = "Tech Owner", PreviousValue = previousTechOwnerName, NewValue = existingProject.TechOwnerName });
}   

NOTE: that all objects are derived from the same object called BaseObj. Also note that I am not just putting the values of the comparable fields into FieldChange object (id versus Name property)

  • 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-06-07T04:21:51+00:00Added an answer on June 7, 2026 at 4:21 am

    You could create a method attribute, for example called ComparableAttribute.
    Then you can decorate all methods in these objects with this attribute.

    You can use reflection in the method you are making the comparison and iterate through all Comparable properties. The code would be much shorter (one iteration, instead of 20 if statements).

    If you need custom information for certain properties, you could specify it via the ComparableAttribute attribute, as parameters.

    The compare method would still take as parameters two instances, but you’ll end up with a much smaller implementation. You could even cache the PropertyInfos for your types, so you don’t reflect at each comparison.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When comparing two objects (of the same type), it makes sense to have a
I am currently working on comparing two complex objects of the same type, with
I am comparing between two tables first column each. If there is find a
I have two objects (instances of the same class) with a bunch of properties,
Example: I have two shared objects (same should apply to .dlls). The first shared
I have two objects that are derived from same the base class. Lets say
Update: Currently looking into NSSET's minusSet links: Comparing Two Arrays Hi guys, Could benefit
I have a really weird issue that I can't figure out with comparing objects
I was stunned to learn that comparing two Boolean Objects with == can get
I am trying to work out a relatively clean way of comparing two types

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.