【问题标题】:C# / DateTime /C# / 日期时间 /
【发布时间】:2019-03-14 03:58:21
【问题描述】:

我的申请页面上有很多活动可供注册。此方法返回所有活动事件。但是有一个问题,比如今天上午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);
    }

【问题讨论】:

  • minDate = startDate.Value.Date; 你认为.Date 会做什么?

标签: c# asp.net datetime


【解决方案1】:

您使用的是startDate.Value.Date,所以它肯定会返回DateTime 的日期部分。如果您还想比较时间部分,请比较完整的DateTime。对于 endDate endDate.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);
}

【讨论】:

    猜你喜欢
    • 2019-09-06
    • 2012-01-20
    • 2012-09-02
    • 2013-03-27
    • 2011-05-30
    • 1970-01-01
    • 2019-06-19
    • 2012-08-03
    • 2018-01-06
    相关资源
    最近更新 更多