LIKE query for DateTime in NHibernate

For a varchar column, I could write a search query like this:

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 can I implement a similar search query for a DateTime column?

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, "19 25.03.2010"), then displays all orders executed during this period of time:
25.03.2010 19:22:00
25.03.2010 19:44:00
25.03 .2010 19:59:00

+2


a source to share


2 answers


Just check if the date is within the 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>();

      

+3


a source


I had a similar problem using LINQ and was fixed with the answer posted by vikram nayak to the question asked here: Nhibernate LINQ DateTime.AddDay doesn't work .

This is how I did it:
I replaced the following classes in vikram's example:

public class ExtendedLinqtoHqlGeneratorsRegistry : DefaultLinqToHqlGeneratorsRegistry
{
    public ExtendedLinqtoHqlGeneratorsRegistry()
    {
        this.Merge(new DateTimeToShortDateStringGenerator());
        //I had to use the lines below instead.
        //MethodInfo method = ReflectionHelper.GetMethodDefinition<DateTime>(x => x.ToShortDateString());
        //RegisterGenerator(method, new DateTimeToShortDateStringGenerator());
    }
}

public class DateTimeToShortDateStringGenerator : BaseHqlGeneratorForMethod
{
    public DateTimeToShortDateStringGenerator()
    {
        SupportedMethods = new[]
        {
            ReflectionHelper.GetMethodDefinition<DateTime>(x => x.ToShortDateString())
        };
    }

    public override HqlTreeNode BuildHql(MethodInfo method, System.Linq.Expressions.Expression targetObject, ReadOnlyCollection<System.Linq.Expressions.Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
    {
        return treeBuilder.MethodCall("DateTimeToShortDateString", visitor.Visit(targetObject).AsExpression());
    }
}

public class CustomDialect : MsSql2008Dialect
{
    public CustomDialect()
    {
        RegisterFunction(
            "DateTimeToShortDateString",
            new SQLFunctionTemplate(
            NHibernateUtil.DateTime,
            "CONVERT(NVARCHAR(10), ?1, 105)"
            )
        );
    }
}

      



Then you can use the following syntax:

using (session.BeginTransaction())
{
    var patients = session.Query().Where(p => p.BirthDate.ToShortDateString().Contains("1975"));
}
      

0


a source







All Articles