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 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

Related Questions

Given a method signature: public bool AreTheSame<T>(Expression<Func<T, object>> exp1, Expression<Func<T, object>> exp2) What would
These two methods exhibit repetition: public static Expression<Func<Foo, FooEditDto>> EditDtoSelector() { return f =>
To make a JavaScript class with a public method I'd do something like: function
The first thing I do in a public method is to validate every single
I have this method: public bool CanExecute() And after 70 commits, I added an
I'm using the Entity Framework and I developed this extension method: public static IQueryable<TResult>
Suppose I have BaseClass with public methods A and B, and I create DerivedClass
Is it possible to make a public method private in a subclass? I don't
While refactoring some old code I have stripped out a number of public methods
I am using the following methods: public void M1(Int32 a) { // acquire MyMutex

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.