score:2

Accepted answer

Try refactoring the IsNullOrEmpty condition like this:

return (from r in Repository.Query<Measurement>()
    where
        (string.IsNullOrEmpty(postalCode)
            || r.Postal.ToLowerInvariant() == postalCode.ToLowerInvariant()
        )
        &&
        (string.IsNullOrEmpty(trait)
            || r.Trait.ToLowerInvariant() == trait.ToLowerInvariant()
        )
    select r).ToList();

That may cause LINQ to evaluate the IsNullOrEmpty before sending off the query. If not, you could precalculate them manually and put a couple boolean variables in their place.

score:1

Have you tried forcing the deferred execution by calling Repository.Query().ToList() before the where clauses? I noticed it looks like NHibernate is attempting to convert the string.IsNullOrEmpty() call into SQL syntax (and failing).

return (from r in Repository.Query<Measurement>().ToList()
        where
            r.Postal.ToLowerInvariant() ==
                (string.IsNullOrEmpty(postalCode)
                    ? r.Postal : postalCode).ToLowerInvariant()
            &&
            r.Trait.ToLowerInvariant() ==
                (string.IsNullOrEmpty(trait)
                    ? r.Trait : trait).ToLowerInvariant()
        select r).ToList();

Related Articles