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