简体   繁体   中英

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

EDIT - 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一些应该返回的示例行,eta 日期是第一个日期字段

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM