-1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Brian Crist
  • 806
  • 3
  • 16
  • 42
  • 1
    `e.StartTime.ToString("yyyy-MM-dd")` – JohnyL Dec 24 '17 at 15:10
  • Possible duplicate of [How to compare only date components from DateTime in EF?](https://stackoverflow.com/questions/1478215/how-to-compare-only-date-components-from-datetime-in-ef) – Roman Koliada Dec 24 '17 at 15:32

1 Answers1

2

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();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Dear @marc_s , as you see in my case, i want to compare this as a string, so i try to use ```"2017-12-15"``` , I confused that i like use string. – Brian Crist Dec 24 '17 at 15:19
  • 2
    @Brian Crist, why are you insistent about comparing dates as string ? It is not proper way. – lucky Dec 24 '17 at 15:25