Converting HQL to NHibernate LINQ

 

I was playing around with converting some HQL and Criteria queries to LINQ, and the result is pretty slick:

All I had to do was reference NHibernate.Linq and I went from:
 
        public IList<Stay> GetCurrentStays()
        {                       
            string hql = @"FROM Stay WHERE CheckinDate <= :now 
                    AND (CheckoutDate IS NULL OR CheckoutDate >= :now)";

            var query = getQuery(hql);
            query.SetDateTime("now", DateTime.Now);
            return query.List<Stay>();
        }
to:     
        public IList<Stay> GetCurrentStays()
        {
            var query = from stay in session.Linq<Stay>()
                        where stay.CheckinDate <= DateTime.Now &&
                              stay.CheckoutDate >= DateTime.Now
                        select stay;
            return query.ToList();
        }    
 
 
 
Goodbye Magic Strings.  Goodbye having to teach my team a another esoteric API and syntax.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Comments

# re: Converting HQL to NHibernate LINQ
Gravatar It looks like the first sample is handling the scenario that CheckoutDate is not specified. but the linq scenario would require it. Is that true or did I miss something?
Left by Dennis Burton on 6/22/2009 10:52 AM
# re: Converting HQL to NHibernate LINQ
Gravatar I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.
Left by oem auto parts on 11/16/2009 7:08 AM
# re: Converting HQL to NHibernate LINQ
Gravatar ASCII HTML codes allow you to enter characters into a web page that are not found on you keyboard. The codes can also be very useful for entering characters for other languages, special characters and you can use them just
for fun.
Left by how to play online poker games on 12/17/2009 7:03 AM
# re: Converting HQL to NHibernate LINQ
Gravatar nice aritclesI found NetSpell to be an excellent free spelling engine that works both in ASP.Net and Windows forms.we provide best static caravan Insurance,Park home insurance and for Holiday lodges too.
Left by static caravan insurance on 2/27/2010 1:15 AM
# re: Converting HQL to NHibernate LINQ
Gravatar Excellent read of post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work... we are major supplier of used clothing, used shoes, garments and many different kinds of name brand clothing closeouts etc., many more are available here...
Left by used cloths on 2/27/2010 1:16 AM
# re: Converting HQL to NHibernate LINQ
Gravatar Is there anyway to convert a hql to a live IQueryable (not executed)? I am trying to make a fluent api for my application.
Left by Bradenton Website Design on 3/12/2010 12:01 AM
# re: Converting HQL to NHibernate LINQ
Gravatar Another proof of the great job performed here. So many great ideas can be found here, Credential inflation as well as College stuff, so thanks a lot.
Left by Credential inflation on 5/23/2010 5:44 PM
# re: Converting HQL to NHibernate LINQ
Gravatar nice aritclesI found NetSpell to be an excellent free spelling engine that works both in ASP.Net prada sunglasses and Windows forms.we provide best static caravan Insurance,Park home insurance and for Holiday lodges too.
Left by gaoxiaofeng on 1/6/2011 2:12 AM

Leave Your Comment

Title*
Name*
Email (never displayed)
 (will show your gravatar)
Url
Comment*

 

Preview Your Comment.