Reputation: 507
I have 2 textboxes to get DateTime.
For example i have today's values on my database and other DateTime values.
When i choose today's date to query, i can't get the today's Date values from database to show. If i choose tomorrow i can get what i want.
I mean when i choose 01.01.2014 - 09.12.2014 - i can get 8 data.(it's incorrect) If i choose 01.01.2014 - 10.12.2014 - i can get 17 data(it's correct)
So i can not get "8" - 09.12.2014 date data even i choose 09.12.2014.
Is there a problem with my query?
@Html.TextBox("min", min, "{0:dd.MM.yyyy}", new { @class = "date", id = "min", @readonly = "true" })
@Html.TextBox("max", max, "{0:dd.MM.yyyy}", new { @class = "date", id = "max", @readonly = "true" })
My model property:
public DateTime DateTime{ get; set; }
My query:
model= (MyBL.GetAllValues().Where(a => a.DateTime>= min && a.DateTime<= max && a.DeptId== myDeptId)).ToList();
Upvotes: 0
Views: 327
Reputation: 269
The answer above will throw an exception since Date property is not supported by LINQ to Entities. You can use EntityFunctions.TruncateTime method. See answers How to compare DateTime without time via LINQ? and The specified type member 'Date' is not supported in LINQ to Entities Exception
Upvotes: 1
Reputation: 21881
It is probably caused by the fact that max
= 09.12.2014 0:00
, i.e. midnight. so 09.12.2014 1:00
for example is after max
. You probably want to only compare the dates, you may try this:
MyBL.GetAllValues().Where(a => a.DateTime.Date >= min.Date && a.DateTime.Date <= max.Date && a.DeptId== kullanicininBirimi)
Upvotes: 2