0

I am trying to retrieve records based on a specific date field. My criteria is to only show records whose values do not fall within current month or greater than today's date.

I already coded a criteria that is working for getting records with future dates. Could someone please assist me on how to solve the problem with showing records that are not in current month.

Thank you very much!

var criteria = Session.CreateCriteria<GLEntry>();
criteria.Add(Expression.Gt("EffectiveDate", DateTime.Today));
ssokol91
  • 552
  • 3
  • 13
  • 25

1 Answers1

2

Just need to to a less than with the first day of the current month.

var firstOfMonth = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
criteria.Add(Expression.Lt("EffectiveDate", firstOfMonth));
dotjoe
  • 26,242
  • 5
  • 63
  • 77
  • Thanks! Spot on! One thing is that it only brings back the records if I comment out the first criteria. How do you do an Or with these two? – ssokol91 Nov 12 '12 at 19:50
  • an `or` can be done with a disjunction...`criteria.add(Restrictions.Disjunction().Add(criteria1).Add(criteria2))` – dotjoe Nov 12 '12 at 19:54
  • Would it actually be an AND instead of OR? Thanks again! – ssokol91 Nov 12 '12 at 19:59
  • `Conjunction` is the `and` version...when you add more criteria, they are all being and'd together by default. The `or` is a more unique case so you can only do that with a disjunction. – dotjoe Nov 12 '12 at 20:03
  • I went ahead and tried this, and it worked. Thanks again for your help! Best wishes! `criteria.Add(Expression.Or(Expression.Gt("EffectiveDate", DateTime.Today), Expression.Lt("EffectiveDate", firstOfMonth)));` – ssokol91 Nov 12 '12 at 20:10