c# - C# / 日期时间 /
问题描述
我的申请页面上有很多活动可供注册。此方法返回所有活动事件。但是有一个问题,比如今天上午11点有一个活动。上午 11 点之后,它在逻辑上不应该显示,但它显示为活动。它直到午夜之后才隐藏起来。因此,当日期更改时(即期末不考虑小时数),它会变为非活动状态......更改 DateTime 以使事件在给定小时后变为非活动状态的最佳方法是什么,而不是明天?
public IPagedList<Domain.Event> SearchEventsPublic(long[] eventCategoryTypeIds = null,
long[] locationIds = null,
DateTime? startDate = null,
DateTime? endDate = null,
int pageIndex = 0,
int pageSize = int.MaxValue)
{
var query = _eventRepository.Table;
// get event by filter
if (eventCategoryTypeIds != null && eventCategoryTypeIds.Length > 0)
query = query.Where(c => eventCategoryTypeIds.Contains(c.EventCategoryId));
if (locationIds != null && locationIds.Length > 0)
query = query.Where(c => locationIds.Contains(c.LocationId));
var minDate = DateTime.Now;
if (startDate.HasValue && startDate.Value > minDate)
{
minDate = startDate.Value.Date;
}
query = query.Where(c => c.StartDateTime >= minDate ||
(c.PrePurchase && (c.ParentId == null || c.ParentId == 0)));
if (endDate.HasValue)
{
var maxDate = endDate.Value.Date.AddDays(1).AddTicks(-1);
query = query.Where(c => c.StartDateTime <= maxDate ||
(c.PrePurchase && (c.ParentId == null || c.ParentId == 0)));
}
query = query.OrderBy(c => c.PrePurchase ? 0 : 1).ThenBy(c => c.StartDateTime);
return new PagedList<Domain.Event>(query.AsQueryable(), pageIndex, pageSize);
}
解决方案
您正在使用startDate.Value.Date
,所以它肯定会返回DateTime
. 如果您还想比较时间部分,请比较完整DateTime
。对于 endDateendDate.Value.Date.AddDays(1).AddTicks(-1);
它也将返回日期部分。您还需要比较完整endDate
的 .
public IPagedList<Domain.Event> SearchEventsPublic(long[] eventCategoryTypeIds = null,
long[] locationIds = null,
DateTime? startDate = null,
DateTime? endDate = null,
int pageIndex = 0,
int pageSize = int.MaxValue)
{
var query = _eventRepository.Table;
// get event by filter
if (eventCategoryTypeIds != null && eventCategoryTypeIds.Length > 0)
query = query.Where(c => eventCategoryTypeIds.Contains(c.EventCategoryId));
if (locationIds != null && locationIds.Length > 0)
query = query.Where(c => locationIds.Contains(c.LocationId));
var minDate = DateTime.Now;
if (startDate.HasValue && startDate.Value > minDate)
{
minDate = startDate.Value;
}
query = query.Where(c => c.StartDateTime >= minDate ||
(c.PrePurchase && (c.ParentId == null || c.ParentId == 0)));
if (endDate.HasValue)
{
var maxDate = endDate.Value.AddDays(1).AddTicks(-1);
query = query.Where(c => c.StartDateTime <= maxDate ||
(c.PrePurchase && (c.ParentId == null || c.ParentId == 0)));
}
query = query.OrderBy(c => c.PrePurchase ? 0 : 1).ThenBy(c => c.StartDateTime);
return new PagedList<Domain.Event>(query.AsQueryable(), pageIndex, pageSize);
}
推荐阅读
- asp.net-core - 为什么 EventLog 提供程序在事件查看器中没有出现任何条目?
- android - 为什么我的 RecyclerView 适配器不工作?
- javascript - 反应应用程序未读取资产文件夹的正确路径-本地图像未显示在本地主机上,仅显示图像图标
- node.js - 两个集合的聚合数组 mongodb
- javascript - 从条件语句中渲染多个 React 组件
- javascript - 将 Django 数据传递给 Vue 组件
- mysql - 使用子查询将价格列与类别的平均价格进行比较
- url - 八度错误:urlwrite:在下载文件时无法打开文件
- node.js - 它可能在docker中使用puppeter和robotjs吗?
- node.js - 为 Atom 开发包时节点模块版本冲突