Today I thought it would be good to try some Linq to nHibernate while I was waiting for VS2010 to finish downloading.

First thing I had to do was download NHibernate 2.1.2 GA and the Linq provider that goes with it.

I was using 2.0.1 up until now. So I had to add a Bytecode provider to my configuration routine. That wasn’t too difficult (I thought).

This was the line I used.

_Configuration.SetProperty(Environment.ProxyFactoryFactoryClass, "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu")
And… it didn’t work. Strange. I was sure I added the references to the Linfu provider but still it gave me this error.

> Unable to load type ‘NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu’ during configuration of proxy factory class.
> Possible causes are:
> – The NHibernate.Bytecode provider assembly was not deployed.
> – The typeName used to initialize the ‘proxyfactory.factory_class’ property of the session-factory section is not well formed.
> Solution:
> Confirm that your deployment folder contains one of the following assemblies:
> NHibernate.ByteCode.LinFu.dll
> NHibernate.ByteCode.Castle.dll

Well, apparently setting a reference doesn’t mean the assembly gets copied over even if you say copy local = true. You have to actually use the assembly to make it copy. Or include it as a file, but I don’t like that idea. 

Adding this somewhere:

Dim i = New NHibernate.Bytecode.LinFu.ProxyFactoryFactory```
will help the process along.

And did you see that VB.Net makes it Bytecode while the assembly uses ByteCode? Groovy.

Now we got that out of the way. 

Then I tried to change this little query.

_Session = SessionFactory.OpenSession
_Query = _Session.CreateQuery("FROM TexCase as e Where e.CaseStatus=:Open")
              _Query.SetEntity("Open",_CaseStatus.FindCaseStatusByCaseStatus("Open") )
_TexCases = _Query.List(Of TexCase)()

It’s a very simple query but the magic string bothers me.

In Linq it looks like this.

vbnet _Session = SessionFactory.OpenSession Dim _Query = From e In _Session.Linq(Of TexCase)() Where e.CaseStatus.Equals(_CaseStatus.FindCaseStatusByCaseStatus("Open")) Select e _TexCases = _Query.ToList Not really much shorter but a lot more friendly when it comes to refactoring. And the result is exactly the same. I like it so far.