Fetching records by date with only day part comparison using nhibernate

前端 未结 3 1311
一生所求
一生所求 2021-01-18 06:51

I would like to fetch all records from particular day, no matter what time is associated with those records. So far I have method like this:

public IQueryabl         


        
3条回答
  •  清酒与你
    2021-01-18 07:36

    As said in the comments, your LINQ query works fine with NH 3.3.

    In earlier releases, you can use HQL:

    return session.CreateQuery("from MyClass where date(MyDate) = :day")
                  .SetParameter("day", day.Date)
                  .List(); //executes
    

    You can also use the date function from Criteria, via SqlFunction. This is more convoluted, but allows building more dynamic queries:

    return session.CreateCriteria()
                  .Add(Restrictions.Eq(
                           Projections.SqlFunction("date",
                                                   NHibernateUtil.Date,
                                                   Projections.Property("MyDate")),
                           day.Date))
                    .List(); //executes
    

提交回复
热议问题