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.

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 `It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again.
Left by links of london Sweetie Bracelet on 10/19/2009 9:48 PM
# re: Converting HQL to NHibernate LINQ
Gravatar Just continue writing this kind of post. I will be your loyal reader. Thanks again.
Left by street lights on 11/12/2009 1:51 AM
# re: Converting HQL to NHibernate LINQ
Gravatar NHibernate.Linq and I went from:
Left by christian louboutin on 11/12/2009 11:30 PM
# re: Converting HQL to NHibernate LINQ
Gravatar iption>Henry will be
Left by christian louboutin sale on 11/16/2009 1:27 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 It looks like the first sample is handling the scenario that CheckoutDate is not specified. but the linq scenario would require it.
Left by tower defense games on 11/17/2009 6:21 AM

Leave Your Comment

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

Preview Your Comment.