1

Many problems today! I can't figure this out so I thought I'd throw it out there.

I am writing a fluent interface to build an SQL string (yes I know about LINQ to Entities...it's a long story).

I am checking the type of expression in an if/else clause, like this:

if (expression is MemberExpression)
{
    var memberExpression = expression as MemberExpression;
    StringBuilder sb = new StringBuilder();

    ...

    return sb.ToString();
}

if (expression is MethodCallExpression)
{
    var methodCallExpression = expression as MethodCallExpression;
    return GetParameterString(Expression.Lambda(expression).Compile().DynamicInvoke().ToString());
}

if (expression is ConstantExpression)
{
    var constantExpression = expression as ConstantExpression;
    return GetParameterString(constantExpression.Value.ToString());
}

When I run the following through this code:

.Where(j => j.Driver.Chatiness == searchResource.Chatiness)

...the "searchResource.Chatiness" is activating the MemberExpression code, when ought to be acivating the ConstantExpression (since searchResource is an object containing a Chatiness property).

Why is this? I've got a few ideas of how I can work around it, but I don't want to work around it, I want it to work!

svick
  • 236,525
  • 50
  • 385
  • 514
serlingpa
  • 12,024
  • 24
  • 80
  • 130

1 Answers1

1

Accessing a field or property would be a MemberExpression. A ConstantExpression would be a constant, like:

.Where(j => j.Driver.Chatiness == 5)
Dennis_E
  • 8,751
  • 23
  • 29
  • Thanks for that Dennis_E. I knew that of course ;) I just seem to remember in the first iteration of this code that expressions such as .Where(j => j.Driver.Chatiness == searchResource.Chatiness) where hitting the MemberExpression. Don't know why. It's fixed now anyway. I discovered how to compile the expression. – serlingpa May 20 '14 at 13:52