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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:15:36+00:00 2026-05-10T20:15:36+00:00

Given a method signature: public bool AreTheSame<T>(Expression<Func<T, object>> exp1, Expression<Func<T, object>> exp2) What would

  • 0

Given a method signature:

public bool AreTheSame<T>(Expression<Func<T, object>> exp1, Expression<Func<T, object>> exp2) 

What would be the most efficient way to say if the two expressions are the same? This only needs to work for simple expressions, by this I mean all that would be ‘supported’ would be simple MemberExpressions, eg c => c.ID.

An example call might be:

AreTheSame<User>(u1 => u1.ID, u2 => u2.ID); --> would return true 
  • 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. 2026-05-10T20:15:36+00:00Added an answer on May 10, 2026 at 8:15 pm

    Hmmm… I guess you’d have to parse the tree, checking the node-type and member of each. I’ll knock up an example…

    using System; using System.Linq.Expressions; class Test {     public string Foo { get; set; }     public string Bar { get; set; }     static void Main()     {         bool test1 = FuncTest<Test>.FuncEqual(x => x.Bar, y => y.Bar),             test2 = FuncTest<Test>.FuncEqual(x => x.Foo, y => y.Bar);     }  } // this only exists to make it easier to call, i.e. so that I can use FuncTest<T> with // generic-type-inference; if you use the doubly-generic method, you need to specify // both arguments, which is a pain... static class FuncTest<TSource> {     public static bool FuncEqual<TValue>(         Expression<Func<TSource, TValue>> x,         Expression<Func<TSource, TValue>> y)     {         return FuncTest.FuncEqual<TSource, TValue>(x, y);     } } static class FuncTest {     public static bool FuncEqual<TSource, TValue>(         Expression<Func<TSource,TValue>> x,         Expression<Func<TSource,TValue>> y)     {         return ExpressionEqual(x, y);     }     private static bool ExpressionEqual(Expression x, Expression y)     {         // deal with the simple cases first...         if (ReferenceEquals(x, y)) return true;         if (x == null || y == null) return false;         if (   x.NodeType != y.NodeType             || x.Type != y.Type ) return false;          switch (x.NodeType)         {             case ExpressionType.Lambda:                 return ExpressionEqual(((LambdaExpression)x).Body, ((LambdaExpression)y).Body);             case ExpressionType.MemberAccess:                 MemberExpression mex = (MemberExpression)x, mey = (MemberExpression)y;                 return mex.Member == mey.Member; // should really test down-stream expression             default:                 throw new NotImplementedException(x.NodeType.ToString());         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I often use PHP's parse_ini_file for this. So if you… May 11, 2026 at 6:37 am
  • added an answer I find LINQ to Objects useful all over the place.… May 11, 2026 at 6:36 am
  • added an answer Based on the stack trace and reflector it looks like… May 11, 2026 at 6:36 am

Top Members

Trending Tags

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

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.