1

I'm applying a .Where()-restriction on an IQueryOver<T,T> in FluentNH, as such:

.Where(x => x.Col1 == null || x.Col1 == "");

Which generates the following SQL:

WHERE (Col1 IS NULL OR Col1 = NULL)

How can I make NH understand that empty string means empty string?

1
  • I haven't ever seen this. Which version of NHibernate are you using? Are you sure that the code is like that? Commented May 15, 2015 at 15:02

1 Answer 1

1

You can write your Where clause like this:

.Where(Restrictions.On<ClassType>(obj => obj.Col1).IsNull ||
       Restrictions.On<ClassType>(obj => obj.Col1).IsLike(@""))

Alternatively, if you're doing this on several queries you should consider creating a query extension:

public static class QueryExtention {
    public static IQueryOver<E, F> WhereStringIsNullOrEmpty<E, F>(this IQueryOver<E, F> query, Expression<Func<E, object>> expression) {
        var property = Projections.Property(expression);
        var criteria = Restrictions.Or(Restrictions.IsNull(property),
                                       Restrictions.Eq(property, string.Empty));
        return query.Where(criteria);
    }
}

Then you should be able to create something like:

.QueryOver<ClassType>()
.WhereStringIsNullOrEmpty(obj => obj.Col1)
Sign up to request clarification or add additional context in comments.

2 Comments

Kay. Seems a lot more verbose. Is there any specific reason why the ORM translates "" to NULL?
@AndreasEriksson Not sure about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.