Impulse Impulse - 21 days ago 6
C# Question

Linq DateTime Operations Comparison

So I have this statement

SearchResults = (from s in dbContext.tbl_ShippingProgram
where s.eta_date.Date > DateTime.Now.Date.AddMonths(-TimePeriod.Value)
select s).ToList();

Where SearchResults is defined as

public List<tbl_ShippingProgram> SearchResults { get; set; }

And where TimePeriod.Value is a int

I'm having difficulties figuring out why this doesn't work, there's nothing wrong the with just selecting all values since

SearchResults = (from s in dbContext.tbl_ShippingProgram
select s).ToList();

Works perfectly fine. Any and all help is appreciated <3

- The issue is that it's not returning anything where it definately should be, checked the values in the db and the calculated DateTime.Now value and it should be returning something
Some Example rows that should be returning, the eta date is the first date field


Try DateTime.Now.AddMonths(-TimePeriod.Value).Date

I think you want the date portion of the datetime after the adjustment. This trips me up all the time too...

Couple of other things to try if this doesn't help:

  1. Calculate the date outside of your linq statement (although you're not getting an unsupported error, but still).
  2. Try using .Compare() for datetimes.
  3. Do some sanity checking by using a hard-coded date that clearly encompass all results.