LinqFu: Dynamic Linq in NHibernate 3.0 (Alpha 3) Linq Provider (part 2 of n)

By following the Dynamic Linq sample from ScottGu’s post, I was able
to use the new Linq provider in NHibernate 3.0 Alpha 3 to create
dynamic Linq statements for NHibernate. The idea is to incorporate
the Visual Expression Builder for Dynamic Linq to allow users to
create their own data filters. Simply copy the Dynamic.cs file from
the DynamicQuery sample and replace the second method
with the following:
public static NhQueryable<T> Where<T>(this IQueryable<T> source,
    string predicate, params object[] values) {
    if (source == null) throw new ArgumentNullException("source");
    if (predicate == null) throw new ArgumentNullException("predicate");
        LambdaExpression lambda = DynamicExpression.ParseLambda(
            source.ElementType, typeof(bool), predicate, values);
        return new NhQueryable<T>(source.Provider,
            Expression.Call(
                typeof(Queryable), "Where",
                new Type[] { source.ElementType },
                source.Expression, Expression.Quote(lambda)));
    }

NOTE: I wasn’t able to get it to work with NHibernate 2.1’s Linq Provider as NHibernateQueryable didn’t inherit from IQueryable. I tried for most of the day to weave my way through it but thought it might just be easier to try the new version of NHibernate and upgrade the references in my app than to create maintenance work for myself in the future.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s