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

Leave Your Comment

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

Preview Your Comment.