I am trying to build a simple query in nHibernate with QueryOver but I want it to convert everything lower-case or ignore sensitive:
Domain.User User = Sessi
public static class QueryOverExtension
{
///
/// This method is used in cases where the root type is required
/// Example: .WhereEqualInsensitive(t => t.Property, stringValue)
///
public static IQueryOver WhereEqualInsensitive(this IQueryOver queryOver, Expression> path, string value)
{
return queryOver.Where(Restrictions.Eq(Projections.SqlFunction("upper", NHibernateUtil.String, Projections.Property(path)), value.ToUpper()));
}
///
/// This method is used in cases where the root type is NOT required
/// Example: .WhereEqualInsensitive(() => addressAlias.DefaultEmail, contactEmailAddress)
///
public static IQueryOver WhereEqualInsensitive(this IQueryOver queryOver, Expression> path, string value)
{
return queryOver.Where(Restrictions.Eq(Projections.SqlFunction("upper", NHibernateUtil.String, Projections.Property(path)), value.ToUpper()));
}
}
Usages:
Session.QueryOver()
.WhereEqualInsensitive(t => t.Property, value)
ChildDTO childAlias = null;
Session.QueryOver()
.JoinAlias(t => t.ChildDTO, () => childAlias)
.WhereEqualInsensitive(() => myAlias.Property, value)