简体   繁体   中英

How to compare date in Entity Framework?

I want to compare date, datetime field in Entity Framework. In my database, I have a column StartTime as date or smalldatetime . Ok, I use this case

var list = db.tmpListAction.Where(e => e.StartTime.ToString() == "2017-12-15").ToList();

It's not working. It returns null although I have data in table.

Why bother converting to a string again?? That most likely breaks your code, since the .ToString() isn't guaranteed to return the data representation you're expecting.

Just leave the date as a date and compare like this:

DateTime desiredDate = new DateTime(2017, 12, 15);
var list = db.tmpListAction.Where(e => e.StartTime == desiredDate).ToList();

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