1

如果这是我在下面尝试做的一个愚蠢的问题,请原谅。

这是我的行动:

public JsonResult CumLeadsParameters(CumLeadsReport cumLeads)
{
    var weeks = (cumLeads.EndDate - cumLeads.StartDate).TotalDays / 7;

    if (!(weeks > 0))
    {
        // means I have less than a week so calculate days and make it as a weeek and 
        var startDate = new DateTime(cumLeads.StartDate.Year,
                                     cumLeads.StartDate.Month,
                                     cumLeads.StartDate.Day, 0, 0, 0, 0);
        var ts = new TimeSpan(23, 59, 59);
        var endDate = startDate.AddDays(6.0).Date + ts;
        var x = _retailerStatsRepository.GetAllRetailersForManufacturerCountrySelectedDates(
                    manufacturer.Id,
                    country.Id,
                    startDate,
                    endDate);
    }
    else
    {
        cumLeads.StartDate = new DateTime(cumLeads.StartDate.Year,
                                          cumLeads.StartDate.Month,
                                          cumLeads.StartDate.Day, 0, 0, 0, 0);
        while (weeks > 0)
        {
            weekCounter++;
            var ts = new TimeSpan(23, 59, 59);
            cumLeads.EndDate = cumLeads.StartDate.AddDays(6.0).Date + ts;
            var x = _retailerStatsRepository.GetAllRetailersForManufacturerCountrySelectedDates(
                        manufacturer.Id,
                        country.Id,
                        cumLeads.StartDate,
                        cumLeads.EndDate);

            tuple.Add(new Tuple<int, IQueryable<RetailerStat>, DateTime, DateTime>(
                weekCounter,
                x,
                cumLeads.StartDate,
                cumLeads.EndDate));
        }
    }
}

/* 笔记*/

例如,我已经通过了 datecumLeads.StartDatecumLeads.EndDateas2013-08-01 to 2013-08-12 然后我的 while 条件被满足了两次,当它第二次进入循环时,我不希望 date 被设置回01/08/2013startdate想要的 as 08/08/2013 00:00:00:000

任何建议都会有所帮助。

4

1 回答 1

1

很少清理我注意到的东西。请注意,我将您的代码格式化为在此站点上看起来更清晰,我并不是要暗示上述更改是您应该做的事情(以下是我的建议)。

  • !(weeks > 0)会更好weeks <= 0
  • new DateTime有一个过载需要year, month, day
  • 更好的是,只.Date返回一个DateTIme带有这些参数的新参数。
  • startDate.AddDays(7).AddSeconds(-1)是您的逻辑的更紧凑版本。请注意,这.Date是多余的,因为您已经指定它是一个日期。

要回答您的问题,您不会cumLeads.StartDate在任何地方更新,如果您希望它更改,则需要更新它。我不知道您要做什么,因此只需执行我上面的观点即可。

public JsonResult CumLeadsParameters(CumLeadsReport cumLeads)
{
    var weeks = (cumLeads.EndDate - cumLeads.StartDate).TotalDays / 7;

    if (!(weeks > 0))
    {
        // means I have less than a week so calculate days and make it as a weeek and 
        var startDate = cumLeads.StartDate.Date;
        var endDate = startDate.AddDays(7).AddSeconds(-1);
        var x = _retailerStatsRepository.GetAllRetailersForManufacturerCountrySelectedDates(
                    manufacturer.Id,
                    country.Id,
                    startDate,
                    endDate);
    }
    else
    {
        cumLeads.StartDate = cumLeads.StartDate.Date;
        while (weeks > 0)
        {
            weekCounter++;
            cumLeads.EndDate = cumLeads.StartDate.AddDays(7).AddSeconds(-1);
            var x = _retailerStatsRepository.GetAllRetailersForManufacturerCountrySelectedDates(
                        manufacturer.Id,
                        country.Id,
                        cumLeads.StartDate,
                        cumLeads.EndDate);

            tuple.Add(new Tuple<int, IQueryable<RetailerStat>, DateTime, DateTime>(
                weekCounter,
                x,
                cumLeads.StartDate,
                cumLeads.EndDate));
        }
    }
}
于 2013-11-07T23:57:43.443 回答