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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:32:22+00:00 2026-06-17T05:32:22+00:00

I have built my own SQL Query builder that breaks apart an Expression, however,

  • 0

I have built my own SQL Query builder that breaks apart an Expression, however, I’m having an issue trying to get the value of string defined in the same function as the lambda expression.

Here is what I am trying to do in console app:

private static void MyBuilderTest()
{
    var sqlBuilder = new SqlBuilder();

    // Doesn't work -- NEED GUIDANCE HERE
    var testValue = "Test";  // Defined in the same function as the lambda below

    sqlBuilder.Select<FooObject>(o => o.FooValue == testValue);

    // Works
    var someObject = new SomeObject { SomeValue = "classTest };

    sqlBuilder.Select<FooObject>(o => o.FooValue == someObject.SomeValue);
}

In my builder it subclasses from ExpressionVisitor, and I override the VisitMember. I found that a string defined in at the base Console level will come back as:

Node.Expression.NodeType == ExpressionType.Constant

The Node.Expression passes back properties of:

CanReduce = false
DebugView = ".Constant<ConsoleApplication1.Program+<>c__DisplayClass1>(ConsoleApplication1.Program+<>c__DisplayClass1)"
NodeType = Constant
Type = System.Type {System.RunetimeType}
Value = {ConsoleApplication1.Program}

The Node.Expression.Value contains:

testValue = "Test" (Type: string)

How do I get this value? I’ve tried several things, like:

var memberType = node.Expression.Type.DeclaringType;

This passes back a ConsoleApplication1.Program type.

However, when I do:

 memberType.GetProperty("testValue");   // Declaring Type from Expression

It passes back null.

The above methods work fine if I place the lambda “strings” in a class, but doesn’t work if they string is defined in the console function.

Can anyone tell me how to get the string value if it’s defined at the function level of the lambda?

EDITED: Added VisitMember

protected override Expression VisitMember(MemberExpression node)
{
    if (node.NodeType == ExpressionType.Constant)
    {
        // Node.Expression is a ConstantExpression type.
        // node.Expression contains properties above
        // And Has Value of:  {ConsoleApplication1.Program}
        // Expanding Value in Watch window shows:  testValue = "Test"

        // How do I get this value, if the ConsoleApplication1.Program type doesn't
        // even know about it?  Looks like maybe a dynamic property?
    }
 }

EDITED

Added code to the console app example to show what works and what doesn’t.

  • 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-17T05:32:23+00:00Added an answer on June 17, 2026 at 5:32 am

    The lambda in your example has “closed over” the testValue variable, meaning the compiler has captured it as a field of the same name in an automatically generated class called ConsoleApplication1.Program+<>c__DisplayClass1>. You can use normal reflection to get the current value of that field by casting the right hand-side of the binary expression into a MemberExpression.

    var testValue = "hello";
    var expr = (Expression<Func<string, bool>>) (x => x == testValue);
    var rhs = (MemberExpression) ((BinaryExpression) expr.Body).Right;
    var obj = ((ConstantExpression) rhs.Expression).Value;
    var field = (FieldInfo) rhs.Member;
    var value = field.GetValue(obj);
    Debug.Assert(Equals(value, "hello"));
    testValue = "changed";
    value = field.GetValue(obj);
    Debug.Assert(Equals(value, "changed"));
    

    Alternatively you can change your variable into a constant.

    const string testValue = "hello";
    var expr = (Expression<Func<string, bool>>) (x => x == testValue);
    var value = ((ConstantExpression) ((BinaryExpression) expr.Body).Right).Value;
    Debug.Assert(Equals(value, "hello"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have our own media player built in Javascript and I was wondering if
Need to use own imaged markers instead built-in pins. I have several questions. 1.
I'm trying to build my own template tags. I have no idea why I
I have built so far an application that allows the user to drag and
I have a WinForm application built with VS 2008 (C#) and SQL Server Express
I'm playing with LINQ-To-SQL in .NET 3.5 and have built some classes based on
I have a data layer built on Linq-2-SQL in .net 3.5. We're looking at
FulePHP is a HMVC framework that allows 'modules' to have their own MVC structure.
I'm trying to query the maximum size of a SQL Azure database using code
I have a web application built in ASP and initially hosted on my own

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.