0

I have an ActionResult where I want to select records based on a date column in SQL Server. This date is in a column of type Date. I can't directly compare the dates since C# DateTime includes the time component and the Date datatype does not. Is there a nice way to do this?

public ActionResult AbsencesByDate(DateTime date)
{
        var absences = from attendance in db.Attendances
                          where attendance.Date == date
                          select new
                          {
                              returnedPersonID = attendance.PersonID,
                             FullName = attendance.Person.FName + " " + attendance.Person.LName,
                          };
        return Json(absences, JsonRequestBehavior.AllowGet);
}

2 Answers 2

2

You could remove the time part from your date parameter in your function. Something like this :

public ActionResult AbsencesByDate(DateTime date)
{
    date = date.Date;
    var absences = from attendance in db.Attendances
                      where attendance.Date == date
                      select new
                      {
                          returnedPersonID = attendance.PersonID,
                         FullName = attendance.Person.FName + " " + attendance.Person.LName,
                      };
    return Json(absences, JsonRequestBehavior.AllowGet);
}
Sign up to request clarification or add additional context in comments.

Comments

1

try using:

where attendance.Date == date.Date

1 Comment

I think this is the easiest solution so I went with this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.