For a column of type varchar I could write such search query:
public IList<Order> GetByName(string orderName)
{
using (ISession session = NHibernateHelper.OpenSession())
{
return session.CreateCriteria<Order>().
Add(Restrictions.Like("Name", string.Format("%{0}%", orderName))).
List<Order>();
}
}
How do I implement the similar search query for a column that has a DateTime type?
public IList<Order> GetByDateTime(string str)
{
using (ISession session = NHibernateHelper.OpenSession())
{
return session.CreateCriteria<Order>()
.Add(Restrictions.Like(Projections.Cast(NHibernateUtil.String, Projections.Property("Created")),
'%' + str + '%'))
.List<Order>();
}
}
That is, if the method is passed the date and part-time (eg "25.03.2010 19"), then displays all orders are carried out in this period of time:
25.03.2010 19:22:00
25.03.2010 19:44:00
25.03.2010 19:59:00
Just check if the date is within your desired range:
DateTime beginDate = new DateTime(2010, 3, 25, 19, 0, 0);
DateTime endDate = new DateTime(2010, 3, 25, 20, 0, 0);
return session.CreateCriteria<Order>()
.Add(Expression.Between("OrderDate", beginDate, endDate))
.List<Order>();