Can I use SQL functions in NHibernate QueryOver?

不羁的心 提交于 2020-01-12 14:12:31

问题


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?


回答1:


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




回答2:


I believe it works at least in the build I am using (version 3.0.0.4000)... below is my example...

var reasons = _session.Query<Reason>();
var myReason = (from r in reasons 
                where r.IsCritical 
                   && r.ReasonCode.ToUpper() == reasonCode.ToUpper() 
               select r).FirstOrDefault();

Give it a shot and let me know if it works for you...



来源:https://stackoverflow.com/questions/5966986/can-i-use-sql-functions-in-nhibernate-queryover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!