Ayende @ Rahien

Refunds available at head office

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.

Comments

Grimace of Despair
03/16/2007 12:45 PM by
Grimace of Despair

That was just an accident waiting to happen. Way to go, Ayende!

Craig Neuwirt
03/16/2007 03:01 PM by
Craig Neuwirt

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

Ayende Rahien
03/16/2007 03:04 PM by
Ayende Rahien

I think you installed the .Net 3.0 redistributable.

You need the March CTP for this

Liang
03/16/2007 11:39 PM by
Liang

Ayende,

The SVN link seems not working.

Ben Scheirman
03/17/2007 02:11 AM by
Ben Scheirman

golf clap

Well done, Ayende.

Ayende Rahien
03/17/2007 09:20 AM by
Ayende Rahien

@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

Frans Bouma
03/17/2007 12:18 PM by
Frans Bouma

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.

Ayende Rahien
03/17/2007 01:27 PM by
Ayende Rahien

Linq() is an extention method that returns an IQueryable.

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.

Jerome Haltom
03/17/2007 02:27 PM by
Jerome Haltom

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(ICollection) 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>

Ayende Rahien
03/17/2007 02:34 PM by
Ayende Rahien

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.

Sam Gentile
03/18/2007 02:02 AM by
Sam Gentile

Yup, I'm still stuck in Seattle and I still feel like crap. Tomas just went off to the airport and I

Jon Stelly
03/18/2007 05:14 PM by
Jon Stelly

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 instead of IQueryable. 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

Ayende Rahien
03/19/2007 09:03 PM by
Ayende Rahien

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!

Console.Write(this.Opinion)
03/20/2007 01:36 PM by
Console.Write(this.Opinion)

Eu sou um leitor compulsivo de blogs e web-sites, mas infelizmente não consigo ler tudo, passo os olhos

Hartmut's Box
03/31/2007 06:20 PM by
Hartmut's Box

In the last days I&#8217;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...

CedarLogic - Shawn Cicoria
04/04/2007 10:12 AM by
CedarLogic - Shawn Cicoria

Here, in the continued foray into LINQ and the 3.5 (Orca's) release, is an implementation of LINQ over...

Comments have been closed on this topic.