Fetching records by date with only day part comparison using nhibernate

前端 未结 3 1315
一生所求
一生所求 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:27

    It depends on the LINQ provider. I'm not sure if NHibernate LINQ provider supports the item.SomeDateProperty.Date == x syntax and I'm doubtful it does. But, you can make your own extension method like so:

    public static IQueryable FilterByDate(this IQueryable This, Expression> getProperty, DateTime date)
    {
      DateTime from = day.Date;
      DateTime to = day.Date.AddDays(1);
      return This.Where(x=> 
        Expression.And(
          Expression.GreaterThan(getProperty, Expression.Variable(from)),
          Expression.LessThan(getProperty, Expression.Variable(to)));
    
    }
    

    This is NOT going to build the way it is now - I was just trying to give you an idea of what to do.

    You can then use it like so:

    var result = repository.Table.FilterByDate(x=>x.MyDate, new DateTime(2012, 6,6));
    

提交回复
热议问题