Ken
Ken

Reputation: 5581

IFNULL in NHibernate

Right now I have something like this in NHibernate:

Expression.Like(property, value, MatchMode.Anywhere)

and that generates SQL like:

property LIKE '%value%'

which is fine for that case. In another case, I want the SQL:

IFNULL(property LIKE '%value%', 0)

but I don't see any example in the manual that refers to IFNULL, nor can I even find any plain-ol' API docs.

I found Expression.Sql(), but only the one example in the manual, and it scares me a little both to use something for which I haven't seen any real docs, and to be doing anything with SQL myself when I'm using NHibernate to try to get away from that.

Is there a better way to do IFNULL in NHibernate than Expression.Sql()?

Upvotes: 1

Views: 777

Answers (2)

Chris Chilvers
Chris Chilvers

Reputation: 6479

I'm assuming this is in a WHERE clause, at which point IFNULL(property LIKE '%value%', 0) is not valid SQL since LIKE evaluates to true or false, and 0 is neither of those.

I suspect you actually want property LIKE '%value%' OR property IS NULL?

If this is the case:

.Add(
    Expression.Disjunction()
    .Add(Expression.Like(property, value, MatchMode.Anywhere))
    .Add(Expression.IsNull(property))
)

Upvotes: 2

Leonel Martins
Leonel Martins

Reputation: 2813

It isnt a answer per se but i suggest you to look for COALESCE(). Here you can see why.

Upvotes: 0

Related Questions