开发者

Can I use SQL functions in NHibernate QueryOver?

开发者 https://www.devze.com 2023-03-05 16:38 出处:网络
I have been searching the internet and can\'t find an example on how to use the queryover of nhibernate 3.0

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开发者_JS百科 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?


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


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号