NHibernate: NullReferenceException on bitwise operations in the where clause

When performing a bitwise operations in the where clause of a LINQ query using the NHibernate LINQ provider e.g.:

from o in SomeObjects where (o.SomeFlags & myBitMask) != myBitMask

you might geht the following exception:

System.NullReferenceException: Object reference not set to an instance of an object.
   at NHibernate.Criterion.SubqueryExpression..ctor(String op, String quantifier, DetachedCriteria dc, Boolean prefixOp) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Criterion\SubqueryExpression.cs:line 30
   at NHibernate.Criterion.Subqueries.Eq(Object value, DetachedCriteria dc) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Criterion\Subqueries.cs:line 130
   at NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitBinaryCriterionExpression(BinaryExpression expr)
   at NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitBinary(BinaryExpression expr)
   at NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp)
   at NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitOrElseExpression(BinaryExpression expr)
   at NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitBinary(BinaryExpression expr)
   at NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp)
   at NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitAndAlsoExpression(BinaryExpression expr)
   at NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitBinary(BinaryExpression expr)
   at NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp)
   at NHibernate.Linq.Visitors.ExpressionVisitor.VisitLambda(LambdaExpression lambda)
   at NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp)
   at NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitUnary(UnaryExpression expr)
   at NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp)
   at NHibernate.Linq.Visitors.RootVisitor.HandleWhereCall(MethodCallExpression call)
   at NHibernate.Linq.Visitors.RootVisitor.VisitMethodCall(MethodCallExpression expr)
   at NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp)
   at NHibernate.Linq.Visitors.NHibernateQueryTranslator.Translate(Expression expression, QueryOptions queryOptions)

I actually even got this exception with a much simpler query:

query
	.Where(
		q =>
			(q.QueryType == null || q.QueryType != xxx) &&
			(q.Variables == null || q.Variables.Trim().Length == 0));

This is due to a bug in NHibernate which seems to be solved in the newer versions of NHibernate. Unfortunately, since I’m using Orchard CMS moving to a newer version of NHibernate is not an option. But the solution is quite simple: You need to convert the query object to a list first and then perform the Where operation. Like this you basically do not rely on any NHibernate class and can workaround the bug:

IEnumerable<LinqQueryPart> query = zzz.List<LinqQueryPart>();
return
	query
		.Where(
			q =>
				(q.QueryType == null || q.QueryType != xxx) &&
				(q.Variables == null || q.Variables.Trim().Length == 0));

Leave a Reply

Your email address will not be published.