2

I have a table which contains a column of datetime type. I want to query this table using Entity FrameWork given a specific datetime which inserted by user using datetimePicker.

The problem is in c# there is no Date type, only DateTime exists when ever is inserted it gets the datetime of that time, for example 24/8/2012 13:02;12, but when get the value from DatetimePicker it gives me 24/8/2012 12:00:00 the two date aren't equal because of their times, and Entity Framework, so how to query on date using only date without time , or any alternative idea will be helpful

That is my code

DateTime dt = new DateTime(2012,8,24);
var data = db.Tabel1.where(x=>x.Date.equals(dt));
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rabar kareem
  • 630
  • 9
  • 28

2 Answers2

3
var Tomorrow = dt.Date.AddDays(1);
var data = db.Tabel1.where(x=>x.Date >= dt.Date && x.Date < Tomorrow);
dotNET
  • 33,414
  • 24
  • 162
  • 251
3

I believe you need to use EntityFunctions.TruncateTime as in:

DateTime dt = new DateTime(2012,8,24);
var data = db.Tabel1.where(x=> EntityFunctions.TruncateTime(x.Date) == dt);
Alex
  • 13,024
  • 33
  • 62