Complex queries in Simple.Data

OK, this is as much a note for myself as anything, but if there’s any feedback I’d be interested to hear it.

I mentioned earlier that I was struggling with an abstraction of join syntax. The joining itself is not a problem, half the time it can be inferred and it’s not a real stretch to find a way of expressing it when it can’t. The issue I was having was around how to specify table+column combinations within the C# language itself.

People have suggested using LINQ to express queries, but the LINQ Expression system doesn’t work with the dynamic type, and anyway, the lambda syntax would probably be awkward for many of the target audience for the project.

But then it occurred to me that I’m working with DynamicObject-based classes, and those mostly do whatever you tell them to do when you override their virtual functions. And there’s a virtual function TryBinaryOperation, which gets an ExpressionType and an argument which is the right operand, and this function doesn’t have any preconceived notions about what value is going to come out of it at all.

This is to say, that in this code…


dynamic x = new DynamicType();
var result = x == 1;

…there is no rule which says that result is going to be a bool.

So, with that in mind, how’s this for complex query syntax:


var activeCustomersInLondon = db.Customers
.FindAll(db.Customers.Active == "Y" & db.Customers.Address.Town == "London");

Note the use of the bitwise AND operator, which is a bit odd, but it turns out that the runtime can’t resist trying to evaluate the left side of an && expression as a bool before asking it how to AND itself with the right side.

Again, any comments or feedback would be appreciated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: