Linq for NHibernate
The code that I am going to show is working code. I have it passing tests on the March 2007 CTP, against NHibernate 1.2 CR1.
Select entity and filter:
var query = (
from user in session.Linq<User>()
where user.Name == "ayende"
select user
).ToList();
Project a property:
var query = (
from user in session.Linq<User>()
select user.Name
).ToList();
Project anonymous type:
var query = (
from user in session.Linq<User>()
select new { user.Name, user.RegisteredAt }
).ToList();
Where do you get that?
You can get the source from Subversion:
svn co https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/experiments/NHibernate.Linq/
You can also download it from the download page.
Caveats:
- It is by no means a complete Linq implementation. The 101 examples for Linq are a good test metric to go against, in my opinion, and I am only hitting 13 of them right now (probably more, but I have tests for 13).
- I read the C# 3.0 spec when it was out in PDC '06, and last night I got the March CTP and started working on an implementation. This is literally my baby steps with Linq.
- I am not going to invest a lot of time in this. It is still far into the future, and I mostly did it to make people stop wishing for it.
Important - patches:
I would appriciate it if you will send bug reports with patches to fix them.
Have fun, and don't make too much out of it.