c# - Linq to Sql 查询,datediff +-3 天

标签 c# asp.net entity-framework linq-to-sql

我正在处理航类页面。现在我的数据库中有一些航类,我想显示它们。 通过此查询,我将显示所选日期的所有航类(asp:calendar 控件):

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-10),我希望查询在表中搜索并显示 +-3 天间隔内的所有航类从选定的日期。有什么建议吗?

最佳答案

您只需计算出开始日期和结束日期,然后在 where 子句中使用比较:

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

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

关于c# - Linq to Sql 查询,datediff +-3 天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26057310/

相关文章:

asp.net - 尝试添加映射文件时ASP.NET脚手架出现问题

c# - 0x800401f3 COMInterop Dll 类字符串无效

c# - 如何阻止 GridView 中的特定列

c# - 组合字符串时方法语法中的 Where 子句失败

asp.net - ASP.Net MVC 中的 PRG 模式?

c# - 使用 ASP.NET 在 Windows Azure Blob 存储上设置 CORS

c# - 使用 EntityConnection、EntityCommand 和 EntityDataReader 时出现 EntitySqlException

c# - 在c#中反序列化json数组列表

c# - 伪造 GetCallingAssembly() 以指向不同的程序集

c# - 获得一个随机数,只要它不在列表中