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:
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.
Comments
That was just an accident waiting to happen. Way to go, Ayende!
Very cool! Is the c# 3.0 redistributable download the only thing
needed to get it to compile. After I installed it, the csc compiler still
did not recognize c# 3.0 extensions.
thanks
craig
I think you installed the .Net 3.0 redistributable.
You need the March CTP for this
I am putting a post together on what we saw from Linq to Entities this week, but let's just say that
Ayende,
The SVN link seems not working.
golf clap
Well done, Ayende.
@Liang,
For some reason is seems to add a :9443 port to the URL, even though it isn't there.
Please try to check it out rather than browse, it will work
You're using extension methods or IQueryable?
You can implement Linq support with extension methods pretty easily, that's not the problem. The problem is optimizing the query, which can only be done by using IQueryable as that gives you the full expression tree, so you can handle multiple selects and froms in a single query for example.
I talked about this with Matt Warren and Anders about this and they are working on an example about this as it doesn't seem to be trivial. (the expression tree interpretation is pretty nasty and not well (read: not at all) documented.
Linq<T>() is an extention method that returns an IQueryable<T>.
I then process the expression tree. I agree that it is not trivial by far.
The implementation right now is very simple, it can handle single select + where clause, and projections.
Some of the stuff that they have in the 101 examples is flat out crazy in "let us see how far we can push it" fashion.
I don't think that they are going to document it, they may provide a sample provider implementation with at least basic functionality.
Bah. You win.
I was working on this, but wasn't even able to get the CTP installed in time. Good job! You have chosen the exact syntax I was going for also.
As for filtering collections, I think ISession.Linq<T>(ICollection<T>) would be appropiate to wrap the NH collection in an IQueryable.
I would solidify .Linq as a REAL method though and build a patch for NH prime. No reason to go the extension method route with an open project such as NH>
There is actually no point in not going the extention method route.
It is easy to do, and it doesn't require that NHibernate itself would have a dependency on Orcas.
Wait with the collection stuff... I want to finish the basic examples first, before we leap frog Microsoft.
Yup, I'm still stuck in Seattle and I still feel like crap. Tomas just went off to the airport and I
Ayende, this looks like a great start at getting Linq for NHibernate going.
I took the liberty of cleaning up some things:
1) I changed the project type of NHibernate.Linq to a class library and split out the unit tests to a NHibernate.Linq.Tests project.
2) NHibernateLinqQuery now implements IOrderedQueryable<T> instead of IQueryable<T>. This lets users add orderby clauses to the query, though I haven't yet added the functionality to process the orderby statement.
3) I changed the namespace to NHibernate.Linq. I don't know what your plans are but I don't want to see your efforts get lost so I'd like to see this brought into the NHibernate project as an official extension. If it becomes an official extension, then it should probably use the typical extension namespace pattern, hence my change.
4) I added an NHibernateContext class that can be used like the ADO.NET Entity Framework ObjectContext class. See the TestContext class in the Tests project for an example.
http://www.jonstelly.com/files/development/NHibernate.Linq.zip
Also, since I'm sure a lot of people are looking for this right now, I'd like to suggest you post a link in the NHibernate forums to this blog entry and maybe we can get a group together to get this working. I plan on dedicating some time in the next few weeks to adding some functionality and you've got a good foundation here for us to build on.
Good work,
Jon
Jon,
Applied nearly in full, the only thing that I changed was the DataContext class, that is a great idea, but now it needs the session from outside, rather than have it create the session factory & factory, which can be expensive.
Thanks for doing such a lot of work!
Eu sou um leitor compulsivo de blogs e web-sites, mas infelizmente não consigo ler tudo, passo os olhos
About a week or so ago, Ayende released his initial version of Linq for NHibernate that can be used with
In the last days I’ve been thinking about what will happen to NHibernate after LINQ has officially been released. While looking for some resources on the web I stumbled about some posts by Oren Eini aka Ayende Rahien. Ayende is...
Here, in the continued foray into LINQ and the 3.5 (Orca's) release, is an implementation of LINQ over...
Here, in the continued foray into LINQ and the 3.5 (Orca's) release, is an implementation of LINQ over
Comment preview