I’m almost sure it is not ANSI-SQL but, in SQL Server you can do a Like query for an int field.
Using the Like Restriction in NHibernate won’t send a like for an int field, because it is not supported for every DB. I’m migrating an application which uses like for integer fields to find domain objects which some codes starts with a known prefix.
Considering that I’m working now with SQL Server and i don’t think about migrating the app to another database i decided to extend ICriterion to get this using a StartsWith restriction
The work is very easy, basically when i create the expression i convert it in a LikeExpression, and convert the value toString, in SQL if you send a string to a like for a numeric value it will return an empty result set which for me is good enough:
public class StartsWithExpression : AbstractCriterion
private readonly ICriterion realCriterion;
/// Initialize a new instance of the <see cref="EqOrNullExpression" /> class for a named
/// Property and its value.
/// <param name="propertyName">The name of the Property in the class.</param>
/// <param name="value">The value for the Property.</param>
public StartsWithExpression(string propertyName, object value)
realCriterion= new LikeExpression(propertyName, value.ToString(), MatchMode.Start);
public override string ToString()
public override TypedValue GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
return realCriterion.GetTypedValues(criteria, criteriaQuery);
public override IProjection GetProjections()
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery,
IDictionary<string, IFilter> enabledFilters)
return realCriterion.ToSqlString(criteria, criteriaQuery, enabledFilters);
The code is ready in the uNhAddins source.
Next thing i want to do is use this in an Example query and get the EnableLike to work with it, but currently i didn’t find out how to extend Example for this.