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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:51:36+00:00 2026-05-25T17:51:36+00:00

I have the following class Person , with a custom Where method: public class

  • 0

I have the following class Person, with a custom Where method:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public string Where(Expression<Func<Person, bool>> predicate)
    {
        return String.Empty;
    }
}

How can I retrieve the Expression parameter names and values in an enumerable manner?

Person p = new Person();
p.Where(i => i.Name == "Shlomi" && i.Age == 26);

For the purpose of building a string query with parameters attached according to the name and value of the expression.

// Eventually I will convert the above into the following:
string Query = "select * from person where name = @Name AND Age = @Age";

SqlParameter[] param = new SqlParameter[] { 
    new SqlParameter("@Name","Shlomi"),
    new SqlParameter("@Age","26")
};
  • 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-25T17:51:37+00:00Added an answer on May 25, 2026 at 5:51 pm

    I certainly think you should follow StriplingWarrior’s advice and use LINQ to Entities or LINQ to SQL, but for the sake of reinventing the wheel (poorly) I’ll build off of a prior answer of mine.

    // Start with a method that takes a predicate and retrieves the property names
    static IEnumerable<string> GetColumnNames<T>(Expression<Func<T,bool>> predicate)
    {
        // Use Expression.Body to gather the necessary details
        var members = GetMemberExpressions(predicate.Body);
        if (!members.Any())
        {
            throw new ArgumentException(
                "Not reducible to a Member Access", 
                "predicate");
        }
    
        return members.Select(m => m.Member.Name);
    }
    

    Now you need to walk the expression tree, visiting each candidate expression, and determine if it includes a MemberExpression. The GetMemberExpressions method below will walk an expression tree and retrieve each of the MemberExpressions found within:

    static IEnumerable<MemberExpression> GetMemberExpressions(Expression body)
    {
        // A Queue preserves left to right reading order of expressions in the tree
        var candidates = new Queue<Expression>(new[] { body });
        while (candidates.Count > 0)
        {
            var expr = candidates.Dequeue();
            if (expr is MemberExpression)
            {
                yield return ((MemberExpression)expr);
            }
            else if (expr is UnaryExpression)
            {
                candidates.Enqueue(((UnaryExpression)expr).Operand);
            }
            else if (expr is BinaryExpression)
            {
                var binary = expr as BinaryExpression;
                candidates.Enqueue(binary.Left);
                candidates.Enqueue(binary.Right);
            }
            else if (expr is MethodCallExpression)
            {
                var method = expr as MethodCallExpression;
                foreach (var argument in method.Arguments)
                {
                    candidates.Enqueue(argument);
                }
            }
            else if (expr is LambdaExpression)
            {
                candidates.Enqueue(((LambdaExpression)expr).Body);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
I have the following classes: class Person { public string Name { get; set;
I have the following code: class Person { public String Name { get; set;
I have the following list: class Person { public String Name { get; set;
Say i have the following: public class Person{ public int ID {get; set;} public
I have the following class: public class Person { public String Name { get;
I have a custom class: public class Person { public String Name { get;
I have following classes public class Person { public string FirstName { get; set;
Give the following class: public Person { public string Name {get; set;} public string
I have the following classes: public class Person { public String FirstName { set;

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.