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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:53:31+00:00 2026-05-23T01:53:31+00:00

Hello, I am using LINQ and EF with C# 4.0. I have dragged the

  • 0

Hello,

I am using LINQ and EF with C# 4.0.
I have dragged the basic ELMAH table into EF (built and saved many many times).
All is working as one would expect.

But have tried to be too ambitious and need a little help – I am trying to get the Column name from an expression that is passed in as a variable.

What I want is this:

Pass in : x=>x.ErrorId

and get : “ErrorId”

public void GetColumnName(Expression<Func<T, object>> property)
{
  // The parameter passed in x=>x.Message
  // Message works fine (probably because its a simple string) using:
  string columnName = (property.Body as MemberExpression).Member.Name;

  // But if I attempt to use the Guid or the date field then it
  // is passed in as x => Convert(x.TimeUtc)
  // As a result the above code generates a NullReference exception
  // i.e. {"Object reference not set to an instance of an object."}

  // What is the correct code here to extract the column name generically?
  // Ideally in a way that won't bite me again in the future.

}

Thank you for your help!
Dan.

  • 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-23T01:53:32+00:00Added an answer on May 23, 2026 at 1:53 am

    If you need to also decompose simple (or nearly simple) expressions, you’ll need some extra legwork to handle the different situations. Here is some starter code that handles some common cases:

    string GetColumnName<T,TResult>(Expression<Func<T,TResult>> property)
    {
        var member = GetMemberExpression(property.Body);
        if (member == null)
            throw new ArgumentException("Not reducible to a Member Access", 
                                        "property");
    
        return member.Member.Name;
    }
    
    MemberExpression GetMemberExpression(Expression body)
    {
        var candidates = new Queue<Expression>();
        candidates.Enqueue(body);
        while (candidates.Count > 0)
        {
            var expr = candidates.Dequeue();
            if (expr is MemberExpression)
            {
                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);
            }
        }
    
        return null;
    }
    

    Which produces output like:

    GetColumnName((x) => x.X): "X"
    GetColumnName((x) => x.X + 2): "X"
    GetColumnName((x) => 2 + x.X): "X"
    GetColumnName((x) => -x.X): "X"
    GetColumnName((x) => Math.Sqrt(x.Y)): "Y"
    GetColumnName((x) => Math.Sqrt(Math.Abs(x.Y))): "Y"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have the following code. using System; using System.Collections.Generic; using System.Linq; using System.Text;
I'm using Linq to Sql and have a Many-To-Many relation and therefore use a
I have a class: class MyClass(object): @property def myproperty(self): return 'hello' Using mox and
Hello i am using NSOperationQueue to download images in the background. I have created
Hello I am developing one wpf application. I am using linq to sql for
Hello I have the following code to retrieve data from my db using nhibernate
hello, can you help me how to write Linq query (using Entity Framework) for
Hello: I am teaching myself Linq to Sql in C#. Because I am using
Suppose I have the following class: using System; using System.Collections.Generic; using System.Linq; using System.Text;
I have successfuly injected managed DLL's into a .net 3.5 application using a bootloader

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.