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

  • Home
  • SEARCH
  • 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 235529
In Process

The Archive Base Latest Questions

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

I have these data transfer objects: public class Report { public int Id {

  • 0

I have these data transfer objects:

public class Report 
{
    public int Id { get; set; }
    public int ProjectId { get; set; }
    //and so on for many, many properties.
}

I don’t want to write

public bool areEqual(Report a, Report b)
{
    if (a.Id != b.Id) return false;
    if (a.ProjectId != b.ProjectId) return false;
    //Repeat ad nauseum
    return true;
}

Is there a faster way to test if two object with only properties have the same values (something that doesn’t require one line of code or one logical expression per property?)

Switching to structs is not an option.

  • 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-11T20:16:20+00:00Added an answer on May 11, 2026 at 8:16 pm

    How about some reflection, perhaps using Expression.Compile() for performance? (note the static ctor here ensures we only compile it once per T):

    using System;
    using System.Linq.Expressions;
    
    public class Report {
        public int Id { get; set; }
        public int ProjectId { get; set; }
        static void Main() {
            Report a = new Report { Id = 1, ProjectId = 13 },
                b = new Report { Id = 1, ProjectId = 13 },
                c = new Report { Id = 1, ProjectId = 12 };
            Console.WriteLine(PropertyCompare.Equal(a, b));
            Console.WriteLine(PropertyCompare.Equal(a, c));
        }
    }
    static class PropertyCompare {
        public static bool Equal<T>(T x, T y) {
            return Cache<T>.Compare(x, y);
        }
        static class Cache<T> {
            internal static readonly Func<T, T, bool> Compare;
            static Cache() {
                var props = typeof(T).GetProperties();
                if (props.Length == 0) {
                    Compare = delegate { return true; };
                    return;
                }
                var x = Expression.Parameter(typeof(T), "x");
                var y = Expression.Parameter(typeof(T), "y");
    
                Expression body = null;
                for (int i = 0; i < props.Length; i++) {
                    var propEqual = Expression.Equal(
                        Expression.Property(x, props[i]),
                        Expression.Property(y, props[i]));
                    if (body == null) {
                        body = propEqual;
                    } else {
                        body = Expression.AndAlso(body, propEqual);
                    }
                }
                Compare = Expression.Lambda<Func<T, T, bool>>(body, x, y)
                              .Compile();
            }
        }
    }
    

    Edit: updated to handle fields too:

    static class MemberCompare
    {
        public static bool Equal<T>(T x, T y)
        {
            return Cache<T>.Compare(x, y);
        }
        static class Cache<T>
        {
            internal static readonly Func<T, T, bool> Compare;
            static Cache()
            {
                var members = typeof(T).GetProperties(
                    BindingFlags.Instance | BindingFlags.Public)
                    .Cast<MemberInfo>().Concat(typeof(T).GetFields(
                    BindingFlags.Instance | BindingFlags.Public)
                    .Cast<MemberInfo>());
                var x = Expression.Parameter(typeof(T), "x");
                var y = Expression.Parameter(typeof(T), "y");
    
                Expression body = null;
                foreach(var member in members)
                {
                    Expression memberEqual;
                    switch (member.MemberType)
                    {
                        case MemberTypes.Field:
                            memberEqual = Expression.Equal(
                                Expression.Field(x, (FieldInfo)member),
                                Expression.Field(y, (FieldInfo)member));
                            break;
                        case MemberTypes.Property:
                            memberEqual = Expression.Equal(
                                Expression.Property(x, (PropertyInfo)member),
                                Expression.Property(y, (PropertyInfo)member));
                            break;
                        default:
                            throw new NotSupportedException(
                                member.MemberType.ToString());
                    }
                    if (body == null)
                    {
                        body = memberEqual;
                    }
                    else
                    {
                        body = Expression.AndAlso(body, memberEqual);
                    }
                }
                if (body == null)
                {
                    Compare = delegate { return true; };
                }
                else
                {
                    Compare = Expression.Lambda<Func<T, T, bool>>(body, x, y)
                                  .Compile();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are a couple of variants of a rename command,… May 12, 2026 at 12:33 pm
  • Editorial Team
    Editorial Team added an answer There is currently no way to build CLS-compliant assemblies from… May 12, 2026 at 12:33 pm
  • Editorial Team
    Editorial Team added an answer You might want to look at Google Protocol Buffers or… May 12, 2026 at 12:33 pm

Related Questions

I currently represent my Business Layer and Data Layer in a single project in
DTO I'm building a Web application I would like to scale to many users.
Without using DynaForm and it's kin. I would like to use a POJO data
Is it possible to extend LINQ-to-SQL entity-classes with constructor-methods and in the same go;

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.