首页 > 解决方案 > 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);
    }

标签: c#asp.netdatetime

解决方案


您正在使用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);
}

推荐阅读