I am developing an ASP.NET MVC application. I am stuck by a situation and can't move forward. I will explain my scenario.
I have a date column in my database. I'm using a code-first approach. Now when someone adds a new date using DateTime.Now
I want to check the database to count how many rows of data are added with today's date. If it's 5 I want to show an error message to the user who is trying to add new data today. But I can't check today's data as datetime always give result with time.
My date model property is
public DateTime? Date { get; set; }
Current to code achieve the above is
DateTime date2 = DateTime.Now;
var dateresult = dbcontext.Appointments.Where(q => q.Date == date2).Count();
if (ModelState.IsValid && result == 0 && dateresult <= 5)
{}
but the count doesn't return the result. It is always zero.
Can someone tell me how to achieve this? How to get today's data without comparing time part? I have searched many but couldn't find a suitable answer.