I'm working with a flights page. Right now I got some flights in my database and I want to display them. With this query I'm displaying all flights on the selected date (asp:calendar control):
var destinationFlights =
(from a in db.Flights
where a.DepartureAirportId == depId && a.DestinationAirportId == destId && a.DepartureDate == depDate
join Airports a1 in db.Airports on a.Airports.AirportName equals a1.AirportName
join Airports a2 in db.Airports on a.Airports1.AirportName equals a2.AirportName
select a).ToList();
The thing I want to achieve is if I for example choose date (2014-10-10) from the calendar form, I want the query to search in the table and show me all flights in the interval of +-3 days from selected date. Any suggestions?
All you need is to work out the start and end dates, then use comparisons in your where
clause:
var earliest = depDate.AddDays(-3);
var latest = depDate.AddDays(4); // Exclusive
var destinationFlights = ...
where ... && a.DepartureDate >= earliest && a.DepartureDate < latest
...;
See more on this question at Stackoverflow