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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:15:18+00:00 2026-06-12T08:15:18+00:00

I have the following method to compare DTOs. bool Equals<T1, T2>(T1 t1, T2 t2,

  • 0

I have the following method to compare DTOs.

bool Equals<T1, T2>(T1 t1, T2 t2, params Expression<Func<T1, object>>[] accessors)
{
  return !(
    from accessor in accessors 
    select ((MemberExpression) accessor.Body).Member.Name into propertyName 
    let p1 = typeof (T1).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly) 
    let p2 = typeof (T2).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly) 
    let p1val = p1.GetValue(t1, null) 
    let p2val = p2.GetValue(t2, null) 
    where !Equals(p1val, p2val) 
    select p1val
  ).Any();
}

I can call this using (a and b are instances of objects that by convention share the same properties, but that aren’t the same objects):

Equals(a, b, x => x.PropertyOne, x => x.PropertyTwo );

Which compares the the objects property by property, which is fine for most cases.

However, I found a case where I needed to compare objects that had properties of complex types and where I wanted to compare properties on the complex types instead of the objects. Something like this:

Equals(a, b, x => x.ComplexTypeProperty.ChildProp );

I have realised that I need to leave the comfy reflection comparison and enter the Expression land, but the main task here is to be able to express both a property accessor and a property accessor via a complex type property and that’s where I’m lost.

Any pointers would be nice, thanks!

  • 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-12T08:15:19+00:00Added an answer on June 12, 2026 at 8:15 am

    The task is not so complicated:

    1. Determine property path or expressions that are given by expressions. For instance this extension method will give you this:

      public static IEnumerable<string> GetPropertiesNames<T, G>(this Expression<Func<T, G>> pathExpression)
      {
          List<string> _propertyNames = new List<string>();
      
          Expression expression = pathExpression.Body;
      
          if (expression.NodeType == ExpressionType.Convert)
          {
              var convert = (UnaryExpression)pathExpression.Body;
              expression = convert.Operand;
          }
      
          while (expression.NodeType == ExpressionType.MemberAccess)
          {
              MemberExpression memberExpression = (MemberExpression)expression;
                if(!(memberExpression.Member is PropertyInfo)) 
                      throw new InvalidOperationException();
              _propertyNames.Add(memberExpression.Member.Name);
              expression = memberExpression.Expression;
          }
      
          if (expression.NodeType != ExpressionType.Parameter)
              throw new InvalidOperationException();
      
          return _propertyNames;
      }
      
    2. Aggregate expression for second type to create function that will return value:

      var parameter = Expression.Parameter(typeof(T2));      
      var expressionToConvert =  accessors[0]; //for future loop
      
          var propertyChainDescriptor = expressionToConvert.GetPropertiesNames() 
               .Aggregate(new { Expression = (Expression)parameterCasted, Type = typeof(T2)},
                   (current, propertyName) =>
                   {
                       var property = current.Type.GetProperty(propertyName);
                       var expression = Expression.Property(current.Expression, property);
                       return new { Expression = (Expression)expression, Type = property.PropertyType };
                   });
      
          var body = propertyChainDescriptor.Expression;
      
          if (propertyChainDescriptor.Type.IsValueType)
          {
              body = Expression.Convert(body, typeof(object));
          }
      
          var t2PropertyRetriver = Expression.Lambda<Func<T2, object>>(body, parameter).Compile();
      
    3. Now execute method that retrieve values and compare:

          var t1PropertyRetriver = accessor[0].Compile();
          var t1Value = t1PropertyRetriver(t1);
          var t2Value = t2PropertyRetriver(t2);
      
          var areEqual = object.Equals(t1Value,t2Value);
      

    The good idea would be to add some caching of generated methods because compilation process is expensive.

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

Sidebar

Related Questions

I have the following classes and I am trying to call Compare method from
I have the following method: public string Phase(string phase) { return Phase 1; }
I have the following method: public static T ExecuteScalar<T>( string query, SqlConnection connection, params
I have in my project the following method: long compare( long* avar, long comp,
i have following code to parse a JSon response from Facebook.: doInBackground(JSONObject... params) {
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of
I have following method which I am using to load ActiveX control dynamically, Dim
I have the following method, which takes in the name of a file as
I have the following method that returns void and I need to use it
I have the following method where I want to test the event.status property only

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.