Gina
Gina

Reputation:

How do you compare dates in a LINQ query?

I am tring to compare a date from a asp calendar control to a date in the table.... here's what i have... it doesn't like the == ?

var query = from details in db.TD_TravelCalendar_Details
                    where details.StartDate == calStartDate.SelectedDate
                    && details.EndDate == calEndDate.SelectedDate
                    select details;

Upvotes: 0

Views: 315

Answers (2)

bruno conde
bruno conde

Reputation: 48265

What is the Type of details.StartDate and details.EndDate? Is it String? Maybe that's the problem. If the database type is String you should Parse the Date String into a DateTime and compare it with the calendar's selected date then.

Upvotes: 0

Andrew Hare
Andrew Hare

Reputation: 351466

In order for your query to work both details.StartDate and calStartDate.SelectedDate must be typed as System.DateTime.

What error are you receiving? Most likely one of these properties is a string and will need to be parsed into a DateTime instance for comparison.

Upvotes: 1

Related Questions