NHibernate Forge
The official new home for the NHibernate for .NET community

Dynamic LINQ to NHibernate

Even if Linq To NHibernate provider allow us to write query in a strongly type manner, it is sometimes needed to works with property names literally. For example in a RIA application a service can receive a column as a string containing the name of the property to order by. Since Linq to NHibernate is a standard Linq provider, we can leverage a standard dynamic linq parser. This is achieved by using an old code by MS, known as System.Linq.Dynamic. By following the link you will find a download location that point to an almost just a sample project that eventually contains the file Dynamic.cs that contains some extension method allowing to merge literal parts in a type safe linq query.

Let’see an example:

var elist = session.Query<MyEntity>()
              .OrderBy(“Name descending”)
              .Skip(first)
              .Take(count)
              .ToList();

I supposed we have a property called Name on the class MyEntity. The OrderBy taking a string as a parameter is an extension method provided by Dynamic.cs, and in order to have it working you just need to merge the file dynamic.cs in your project and import System.Linq.Dynamic. Of course you will have extension for Where and for other linq operators too.

( cross post from my blog )


Posted nov 17 2011, 02:18 p.m. by felicepollano
Filed under:

Comments

wjkhappy14 wrote re: Dynamic LINQ to NHibernate
on 12-02-2011 22:17

It is very good

anchor wrote anchor
on 11-21-2014 5:38

Dynamic LINQ to NHibernate - NHibernate blog - NHibernate Forge

Powered by Community Server (Commercial Edition), by Telligent Systems