if ((!(searchStartDate == default(DateTime))) && (!(searchEndDate == default(DateTime))))
{
requests = requests.Where(x => x.CreatedDate >= searchStartDate && x.CreatedDate <= searchEndDate);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试搜索searchEndDate日期不包括在内时
例如:当开始日期为14/4/15,结束日期为14/4/15时,不返回任何结果,但当开始日期为14/4/15且结束日期为15/4/15时,返回结果15年4月14日
我强烈怀疑的问题是,你的开始和结束日期值在时间上完全一样的点-所以只值在该时间点的确切会被发现。
例如,假设您有:
searchStartDate: 2015-04-15T00:00:00
searchEndDate: 2015-04-15T00:00:00
Sample CreatedDate: 2015-04-15T12:34:56
Run Code Online (Sandbox Code Playgroud)
然后,它们CreatedDate 不会落在searchStartDate和之间searchEndDate。您真的认为它是:
// Look ma, no time of day!
searchStartDate: 2015-04-15
searchEndDate: 2015-04-15
Run Code Online (Sandbox Code Playgroud)
但由于DateTime是一个破碎的模型,你不能表明与类型系统。相反,您基本上需要手动执行此操作:
if (searchStartDate != default(DateTime) && searchEndDate != default(DateTime))
{
// Include the *whole* of the day indicated by searchStartDate
DateTime inclusiveStart = searchStartDate.Date;
// Include the *whole* of the day indicated by searchEndDate
DateTime exclusiveEnd = searchEndDate.Date.AddDays(1);
requests = requests.Where(x => x.CreatedDate >= inclusiveStart
&& x.CreatedDate < exclusiveEnd);
}
Run Code Online (Sandbox Code Playgroud)
您也可以.Date在查询中使用它,但是效率可能会较低,因为它实际上涉及对每个存储值的计算-而我的方法改为仅对边界执行一次计算。