A part of my project is to write a function that will generate an increasing number for every day for every record I have to store in a database table. My problem is that when I try to compare the DateTime.Now with the column I have in my database, I try to compare Datetime with Date so I never have equality on the days. My code explains better than me my issue:
var r = (from i in context.vehicles
where i.InsertionDate == DateTime.Now
select i); // In this query i cant compare the two dates. The one is datetime and the other is date format
int result3 = 0;
if (r.Any())
{
var result = r.OrderByDescending(ii => ii.IncreasingNumberOfTheDay).FirstOrDefault();
int myint = Convert.ToInt32(result.IncreasingNumberOfTheDay);
result3 = myint + 1;
}
else
result3 = 1;
I suspect you just want:
DateTime today = DateTime.Today; // Just fetch the date part.
var query = context.vehicles.Where(v => v.InsertionDate == today);
Avoid string conversions if you possibly can.
If your InsertionDate
includes times, then either you need to try to get the SQL query to truncate that to a date, or you could use:
DateTime today = DateTime.Today;
DateTime tomorrow = today.AddDays(1);
var query = context.vehicles.Where(v => v.InsertionDate >= today &&
v.InsertionDate < tomorrow);
See more on this question at Stackoverflow