2

Is there anyone here with experience writing custom Linq providers?

What I'm trying to do is tell whether a MemberExpression that is a property on a business object should be included in the SQL, or treated as a constant, because its from a local variable that just happens to be a business object.

So for example, if you have this:

Customer c = LoadCustomerFromDatabase();

var orders = from o in db.Orders() where o.CustomerID == c.CustomerID select o;

At the moment, my query translator will try to execute SELECT * FROM orders o where o.CustomerID = c.CustomerID, which of course doesn't work.

What I would like to do is examine the MemberExpression on the c.CustomerID and try to work out if its a local variable, or just something that is being used as part of the Linq expression.

I have managed to do it as a 2nd pass over the query, looking for fields that SQL Server won't be able to bind, and injecting their values instead, but if possible I'd like to get it all happening at the same time. I tried looking at the expression Type property, and IsAutoClass, but that was just a guess because it contained the word Auto. And it didn't work :)

Alex
  • 2,011
  • 3
  • 21
  • 27
David Wengier
  • 10,061
  • 5
  • 39
  • 43

3 Answers3

1

Well, I don't know if you can cut it down to one pass, but you can get information on the member, and if it coincides with another variable you have declared as being part of the query (in this case "o"), you use it to generate your query.

Otherwise, you would assume it is a constant, and then plug that value in.

Unfortunately, because you can have from statements (in addition to the let statement) in multiple places in the query, it doesn't seem like you can do it in just one pass, since you need to know all of the query variables up front.

casperOne
  • 73,706
  • 19
  • 184
  • 253
1

Okay, after some quick statistical analysis (ie, comparing individual properties manually), DeclaringType, ReflectedType and Namespace are when Lambda parameter is not in scope" fires.

So unless someone comes up with a better answer, that might be all I have to go on.

David Wengier
  • 10,061
  • 5
  • 39
  • 43
1

In a Where expression, you are looking at an Expression<Func<T,bool>> - which means that the outermost lambda should have a single ParameterExpression with type T.

If a comparison relates to the row, it will have (as an ancestor) this ParameterExpression; if it is local variable, it will have (as an ancestor) a ConstantExpression - however, the type of this constant expression will be compiler generated to cope with all of the captured variables used in the expression.

Like so:

using System;
using System.Linq.Expressions;
class Foo
{
    public string Name { get; set; }
    static void Main()
    {
        var exp = (LambdaExpression) GetExpression();
        WalkTree(0, exp.Body, exp.Parameters[0]);

    }
    static void WriteLine(int offset, string message)
    {
        Console.WriteLine(new string('>',offset) + message);
    }
    static void WalkTree(int offset, Expression current,
        ParameterExpression param)
    {
        WriteLine(offset, "Node: " + current.NodeType.ToString());
        switch (current.NodeType)
        {
            case ExpressionType.Constant:
                WriteLine(offset, "Constant (non-db)"
                    + current.Type.FullName);
                break;
            case ExpressionType.Parameter:
                if (!ReferenceEquals(param, current))
                {
                    throw new InvalidOperationException(
                        "Unexpected parameter: " + param.Name);
                }
                WriteLine(offset, "db row: " + param.Name);
                break;
            case ExpressionType.Equal:
                BinaryExpression be = (BinaryExpression)current;
                WriteLine(offset, "Left:");
                WalkTree(offset + 1, be.Left, param);
                WriteLine(offset, "Right:");
                WalkTree(offset + 1, be.Right, param);
                break;
            case ExpressionType.MemberAccess:
                MemberExpression me = (MemberExpression)current;
                WriteLine(offset, "Member: " + me.Member.Name);
                WalkTree(offset + 1, me.Expression, param);
                break;
            default:
                throw new NotSupportedException(
                    current.NodeType.ToString());
        }
    }

    static Expression<Func<Foo, bool>> GetExpression()
    {
        Foo foo = new Foo { Name = "abc" };

        return row => row.Name == foo.Name;
    }    
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900