我在数据库中有两列,project_start和project_end,我正在通过一个报告(用ASP编码)传递两个字段(都是可选的)。
下面的伪代码就是我目前的代码:
(project_start< = search_start AND project_end< = search_start)
和
(project_start< = search_end AND project_end> = search_end)
但是上面没有提到的是在开始日期之前开始但在开始日期之后结束的项目,在开始日期之后但在结束日期之后结束的项目,以及在开始之前开始的项目结束日期之后的日期和结束时间:S
以下是我的代码:
if (Request.QueryString["DateFrom"] != null && Request.QueryString["DateFrom"] != "")
{
DateTime dateFromFilter;
dateFromFilter = DateTime.Parse(Request.QueryString["DateFrom"]);
Q = from qua in qTable
where qua.Start_Date.Date <= dateFromFilter.Date
&& qua.End_Date.Date >= dateFromFilter.Date
select qua;
}
//filter to date
if (Request.QueryString["DateTo"] != null && Request.QueryString["DateTo"] != "")
{
DateTime dateToFilter;
dateToFilter = DateTime.Parse(Request.QueryString["DateTo"]);
Q= from qua in qTable
where qua.Start_Date.Date <= dateToFilter.Date
&& qua.End_Date.Date >= dateToFilter.Date
select qua;
}
答案 0 :(得分:3)
如果您希望项目与搜索范围的任何部分重叠,请尝试以下操作:
project_start <= search_end AND project_end >= search_start
我知道它看起来“太简单了”,但它实际上涵盖了所有情况:
<强>更新强>
处理可选参数:
(search_end IS NULL OR project_start <= search_end) AND
(search_start IS NULL OR project_end >= search_start)
答案 1 :(得分:0)
当然你想要:
(project_start >= search_start AND project_end >= search_start)
AND
(project_start <= search_end AND project_end <= search_end)
假设project_end&gt; project_start,这可以简化为
project_start >= search_start AND project_end <= search_end
你说两个搜索参数都是可选的,在这种情况下,make search_start默认为DateTime.MinValue,search_end默认为DateTime.MaxValue。