I am trying to return some records from the database using this query :
ViewBag.TrafficEnter = _iTrafficRepository.Get().Where(i => i.Type == "ورود" && i.SubmitDateTime == DateTime.Now).ToList();
Here you can see my database records:
But the problem is the query doesn't return any values .why ?
Your comparing the DateTime
values in your database with the value of DateTime.Now
which returns the current time (including milliseconds) and could never match any of the values in the table.
Modify you query to select values in a range
var minDate = DateTime.Today; // returns today's with time component set to 00:00:00
var maxDate = minDate.AddDays(1);
ViewBag.TrafficEnter = _iTrafficRepository.Get()
.Where(i => i.Type == "ورود" && i.SubmitDateTime >= minDate && i.SubmitDateTime < maxDate).ToList();
Assuming today's date is 24th May 2016, it will return all 17 records shown in your image.
The issue with DateTime.Now is that implies a very small date range that is equal to the smallest division of it's precision, in this case 1 millisecond. Unless your record is created with almost exactly the same value as DateTime.Now you'll never get a match.
You have two solutions (that may not be appropriate to your needs);
Firstly rather than matching an exact dateTime, try to match a range eg 1 hour before/after DateTime.Now. This would have the advantage of giving you control over range.
Alternatively you could try matching the date value (DateTime.Now.Date) with the date of your record.
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.