简体   繁体   English

C#/ DateTime /

[英]C# / DateTime /

There are a lot of events available for registration on my applications page. 我的应用程序页面上有很多事件可供注册。 This method returns all active events. 此方法返回所有活动事件。 but there is a problem, for example, there is an event today at 11 am. 但是有一个问题,例如,今天上午11点有一个事件。 after 11 am it logically should not be shown, but it shows as active. 上午11点之后,它在逻辑上不应显示,但显示为活动状态。 And it doesn't hide until after midnight. 它直到午夜才隐藏。 So, it becomes inactive when the date changes (ie the end of the period does not take into account the hours)...What is the best way to change the DateTime so that the event becomes inactive after a given hour, and not the next day? 因此,当日期更改时(即,期末不考虑小时数),它将变为非活动状态。更改日期时间的最佳方法是什么,以便事件在给定的小时后变为非活动状态,而不是明天?

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);
    }

You are using startDate.Value.Date so it will definitely return the date part of DateTime . 您正在使用startDate.Value.Date因此它肯定会返回DateTime的日期部分。 If you want to compare time part also then compare complete DateTime . 如果还要比较时间部分,则比较完整的DateTime For endDate endDate.Value.Date.AddDays(1).AddTicks(-1); 对于endDate endDate.Value.Date.AddDays(1).AddTicks(-1); it will also return date part. 它还将返回日期部分。 You also need to compare full endDate . 您还需要比较完整的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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM