2014-09-26 67 views
2

我正在使用航班页面。现在我在我的数据库中有一些航班,我想要显示它们。 与此查询我显示所选日期的所有航班(ASP:日历控件):Linq to Sql查询,datediff + -3天

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(); 

我想实现的事情是,如果我例如可供选择的日期(2014年10月)日历表单,我希望查询在表格中搜索,并在选定日期的+ 3天内向我显示所有航班。有什么建议么?

+0

你尝试搜索?你有没有找到'DateTime.AddDays()'? – CodeCaster 2014-09-26 10:23:02

回答

8

所有你需要的是制定出开始和结束日期,然后在您的where子句中使用比较:

var earliest = depDate.AddDays(-3); 
var latest = depDate.AddDays(4); // Exclusive 

var destinationFlights = ... 
    where ... && a.DepartureDate >= earliest && a.DepartureDate < latest 
    ...; 
+0

这是要走的路,因为(根据我的经验,至少)LINQ提供程序通常不会将'DateTime'方法映射到相应的SQL函数,而无需自定义扩展。 – bstenzel 2014-09-26 10:27:39

+0

这解决了我的问题,谢谢! – Kriistiian 2014-09-26 10:59:54