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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:13:41+00:00 2026-05-11T18:13:41+00:00

public Method1(Expression<Func<T, TProperty>> valueToCompare) { //Examine expression } public Method1(TProperty valueToCompare) : this(x =>

  • 0
public Method1(Expression<Func<T, TProperty>> valueToCompare) {
    //Examine expression
}

public Method1(TProperty valueToCompare) : this(x => valueToCompare) {}

and i run them like this

Method1(x => 1);

and

Method1(1);

If i examine the expression when the first overload is called then I get a constant expression. However when I examine the second one I get a member expression.

The question is how do I get access to the value ‘1’ no matter which overload i call.

Update1:
I used to do this until I realised the didn’t both return the same thing.

if (valueToCompare.Body.NodeType == ExpressionType.Constant)
{
    var constant = valueToCompare.Body as ConstantExpression;
    ValueToCompare = constant != null ? (TProperty)constant.Value : default(TProperty);
}

What will i get if I compile it?

Tried to do this but it didn’t work.

if (ValueToCompare .Body.NodeType == ExpressionType.MemberAccess) {
   var member = ValueToCompare .Body as MemberExpression;
   if (member.Expression.NodeType == ExpressionType.Constant)
   {
       ConstantExpression constant = member.Expression as ConstantExpression;
       ValueToCompare = constant.Value;
   }
}

Cheers.

  • 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-11T18:13:41+00:00Added an answer on May 11, 2026 at 6:13 pm

    Are you happy to just compile the expression and run it? That would seem the easiest solution to me…

    Alternatively, you could make your overload taking a TProperty explicitly construct a constant expression, rather than using a lambda expression. It really depends on what you’re trying to achieve.

    As an example of the latter approach, you’d write something like (untested):

    public void Method1(TProperty valueToCompare) : this(x => valueToCompare)
    {
         Expression constant = Expression.Constant(valueToCompare,
                                                   typeof(TProperty));
         ParameterExpression parameter = Expression.Parameter(typeof(T),
                                                              "t");
         Expression lambda = Expression.Lambda<Func<T, TProperty>> (constant,
                                                                    parameter);
         Method1(lambda);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 104k
  • Answers 104k
  • 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
  • Editorial Team
    Editorial Team added an answer I just found a python script that solves my problem… May 11, 2026 at 8:32 pm
  • Editorial Team
    Editorial Team added an answer Try mailto:my@address.com?Subject=www.site.com?val1=a%26val2=b &amp; is an HTML escape code, whereas %26… May 11, 2026 at 8:32 pm
  • Editorial Team
    Editorial Team added an answer Python is a pretty easy language to debug. You can… May 11, 2026 at 8:32 pm

Related Questions

I am trying to write a static function to Or two expressions, but recieve
Consider the following code: string propertyName; var dateList = new List<DateTime>() { DateTime.Now };
Is there a better way to get the Property name when passed in via
I'm working on a method that accepts an expression tree as a parameter, along
Lets say I have something called Stuff in my database, with a property called

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.