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 :)
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.