A few weeks ago, Oren Eini (Ayende Raihan or, as is often referred to) communicated the release of version 1.0 of the Linq provider for NHibernate, a feature highly demanded by users since the advent of integrated query language. NET.
Although it will be included as part of NHibernate product in future versions, have decided to release the current release of the supplier as separate package so you can start to be used from now. It is being tested in many applications in production for several years, and apparently the performance is just right.
And how can help the supplier, if you’re user of NHibernate? The following example from Caffeinated Coder demonstrates how to query database can be simplified and made more readable using Linq and also benefit from strong typing, intellisense and compile-time checks:
Using NHibernate API:
public IList<Call> GetCallsByDate(DateTime beginDate, int interpreterId)
{
ICriteria criteria = Session.CreateCriteria(typeof(Call))
.CreateAlias("Customer", "Customer")
.Add(Restrictions.Gt("StartTime", beginDate))
.Add(
Restrictions.Or(
Restrictions.Lt("EndTime", DateTime.Now), Restrictions.IsNull("EndTime"))
)
.Add(Restrictions.Eq("Interpreter.Id", interpreterId))
.AddOrder(Order.Desc("StartTime"))
.AddOrder(Order.Desc("Customer.Name"));
return criteria.List<Call>() as List<Call>;
}
Using Linq:
public IList<Call> GetCallsByDateWithLinq(DateTime beginDate, int interpreterId)
{
var query = from call in Session.Linq<Call>()
where call.StartTime > beginDate
&& (call.EndTime == null || call.EndTime < DateTime.Now )
&& call.Interpreter.Id == interpreterId
orderby call.StartTime descending, call.Customer.Name
select call;
return query.ToList();
}
You can download both binaries and source code from the SourceForge project page.

