Tweak to that join syntax

I really wasn’t happy with the way you need to break up the method chain to do certain joins, and while writing the release post last night an alternative occurred to me, which I have just spiked and liked:

[sourcecode language="csharp"]dynamic manager; // To hold the aliased reference
var q = _db.Employees.Query()
.Join(_db.Employees.As("Manager"), out manager).On(manager.Id == _db.Employees.ManagerId)
.Select(_db.Employees.Name, manager.Name.As("Manager"));[/sourcecode]

This requires a variable to be declared ahead of the query, which gets passed as an out parameter to the Join method and receives the aliased ObjectReference. That variable can then be used for that reference throughout the rest of the query. It’s still a compromise, but I think it’s a better one.

It still works with named parameter syntax, but because runtime "magic" methods don’t support out parameters in .NET 4, I’ve had to shunt the named parameters into an overload of the On method:

[sourcecode language="csharp"]dynamic manager; // To hold the aliased reference
var q = _db.Employees.Query()
.Join(_db.Employees.As("Manager"), out manager).On(manager.Id == _db.Employees.ManagerId)
.Select(_db.Employees.Name, manager.Name.As("Manager"));[/sourcecode]

I don’t know about you, but I occasionally release a piece of software into production and then, next time I look at the code, think of a better way of achieving something. The nice thing about having good test coverage is that you can make those changes and be confident that what you’ve done hasn’t broken anything.

Anyway, this syntax will be in 0.6.9, along with the Having clause support.

Share on facebook
Share on google
Share on twitter
Share on linkedin


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.