我的对象值如何在迭代中改变?

时间:2013-11-07 23:34:56

标签: c# asp.net-mvc c#-4.0

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

这是我的行动:

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

/ *注意* /

例如,我已将日期cumLeads.StartDatecumLeads.EndDate作为2013-08-01 to 2013-08-12传递 然后我的while条件满足两次,当第二次进入循环时,我不希望将日期设置回01/08/2013作为我的startdate我希望它为08/08/2013 00:00:00:000

任何建议都会有所帮助。

1 个答案:

答案 0 :(得分: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));
        }
    }
}