Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use SQL functions in NHibernate QueryOver?

I have been searching the internet and can't find an example on how to use the queryover of nhibernate 3.0 For example I would like to use the string functions on the where clause of the queryover ex:

var item = Query.Where(x => x.Name.ToLower() == name.ToLower()).FirstOrDefault();

But this doesn't work, because nhibernate can't understand the ToLower, so how can extend the dialect in a way that this becomes possible?

like image 203
Ruben Monteiro Avatar asked May 11 '11 15:05

Ruben Monteiro


1 Answers

session.QueryOver<Foo>()
    .Where(Restrictions.Eq(
        Projections.SqlFunction("lower", NHibernateUtil.String, 
            Projections.Property<Foo>(x => x.Name)),
        name.ToLower()))

should get you SQL like where lower(Name) = @p0

like image 161
dotjoe Avatar answered Oct 21 '22 13:10

dotjoe