Reverse Expression.Like criterion

Posted by Joel Potter on Stack Overflow See other posts from Stack Overflow or by Joel Potter
Published on 2010-05-27T18:14:42Z Indexed on 2010/05/27 18:21 UTC
Read the original article Hit count: 258

How should I go about writing a backwards like statement using NHibernate criteria?

WHERE 'somestring' LIKE [Property] + '%'

Sub Question:

Can you access the abstract root alias in a SQLCriterion expression?


This is somewhat achievable using the SQLCriterion expression

Expression.Sql("? like {alias}.[Property] + '.%'", value, NHibernateUtil.String);

However, in the case of class inheritance, {alias} is replaced with the incorrect alias for the column.

Example (these classes are stored in separate tables):

public abstract class Parent 
{
    public virtual string Property { get; set; }
}

public class Child : Parent { }

The above query executed with Child as the root type will replace {alias} with the alias to the Child table rather than the Parent table. This results in an invalid column exception.

I need to execute a like statement as above where the property exists on the parent table rather than on the root type table.

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about icriteria