在C#中获得最后'N'个宿舍

时间:2011-09-20 12:13:54

标签: c#

假设当前季度是3,年份是2011年。我怎样才能获得最后5个季度

期望的输出:

Q3-2011
Q2-2011
Q1-2011
Q4-2010
Q3-2010

附加Q和' - '。

我正在尝试

int generateQuater = 5;

            int currentQuater = 3;//GetQuarter(DateTime.Now.Month);
            int currentYear = DateTime.Now.Year;

            List<string> lstQuaterYear = new List<string>();

            lstQuaterYear.Add(string.Concat('Q',currentQuater, '-', currentYear));

            for (int i = generateQuater; i > 0; i++)
            {
              //code to be placed   
            }

由于

8 个答案:

答案 0 :(得分:3)

你必须减少你的循环变量。其余的数学并不太难。 它也没有必要以任何特殊的方式处理第一次迭代:

for (int i = generateQuater; i > 0; i--)
{
    lstQuaterYear.Add(string.Format("Q{0}-{1}", currentQuater, currentYear));
    if (--currentQuater == 0)
    {
        currentQuater = 4;
        currentYear--;
    }
}

答案 1 :(得分:1)

作为纯粹的LINQ表达式:

    public IEnumerable<String> GetQuarters(int start, int year, int count)
    {
        return (from q in Enumerable.Range(0, count)
                select String.Format("Q{0}-{1}", (start - q) + (((q + 1) / 4) * 4) , year - ((q + 1) / 4)));
    }

数学有点难看,但确实有用,你可以这么做:

foreach (String quarter in GetQuarters(3, 2011, 5))
{
   Console.WriteLine(quarter);
}

答案 2 :(得分:0)

当你增加i时,你的for循环应该从0变为你的变量。

内部代码可以是:

currentQuarter--;
if(currentQuarter == 0)
{
  currentQuarter = 4;
  currentYear--;
}

答案 3 :(得分:0)

不要忘记重构它:)

        int count = 5;

        int currentQuarter = GetQuarter(DateTime.Now.Month);
        int currentYear = DateTime.Now.Year;

        List<string> lstQuaterYear = new List<string>();

        for (int i = count; i > 0; i--)
        {
            lstQuaterYear.Add(string.Concat('Q', currentQuarter, '-', currentYear));
            currentQuarter--;
            if (currentQuarter == 0)
            {
                currentQuarter = 4;
                currentYear--;
            }
        }

答案 4 :(得分:0)

一种方法是检查年度翻转,然后将季度设置为4并减少年份:

int quarter=3;
int year=2011;
int count=5;
for(int i=0;i<count;i++)
{
   lstQuaterYear.Add(string.Format("Q{0} {1}", quarter, year);
   quarter--;
   if(quarter==0)
   {
     quarter=4;
     year--;
   }
}

或者,您可以计算totalQuartal=year+quartal-1。然后在每一步减少它。最后使用year=totalQuartal/4quartal=totalQuartal%4+1。但我认为第一种方式更容易理解。

答案 5 :(得分:0)

public static IEnumerable Generate(int number, int currentYear, int currentQuarter)
{
    int counter = number;
    int quarter = currentQuarter;
    int year = currentYear;
    while (counter-- > 0)
    {
        yield return String.Format("Q{0}-{1}", year, quarter);
        quarter = quarter>1?quarter-1:4;
        year = quarter==4?year-1:year;
    }
}

答案 6 :(得分:0)

这是我的版本(对不起,它是在VB.NET中)。

这个想法是:

  • 根据日期轻松找出季度(简单:将其除以4 ...并添加1以避免零)
  • 从当前日期返回,每次删除3个月
  • 打印格式化的季度

代码:

Private Shared Function GetQuarterForDate(ByVal d As DateTime) As Integer
    Return (d.Month \ 4) + 1 'integer division
End Function

Private Shared Function GetLastNQuarters(ByVal N As Integer) As IEnumerable(Of String)
    Dim myDate = DateTime.Now
    Dim res As New List(Of String)()
    Do While N > 0
        'using yield would be nicer in C# ... does not exist in VB
        res.Add(String.Format("Q{0}-{1}", GetQuarterForDate(myDate), myDate.Year))
        myDate = myDate.AddMonths(-3)
        N = N - 1
    Loop
    Return res
End Function

<TestMethod()>
Public Sub CanRetrieveQuarter()
    Dim quarters = GetLastNQuarters(5)

    For Each q In quarters
        Console.WriteLine(q)
    Next
End Sub

最后一个“测试方法”打印出来:

Q3-2011
Q2-2011
Q1-2011
Q4-2010
Q3-2010

答案 7 :(得分:0)

如果您应该在季度期间执行某些操作,例如检查时刻是否在一个季度内,您可以使用Time Period Library for .NET季度类:

// ----------------------------------------------------------------------
public ITimePeriodCollection GetPastQuarters( int count )
{
  TimePeriodCollection quarters = new TimePeriodCollection();

  Quarter quarter = new Quarter();
  for ( int i = 0; i < count; i++ )
  {
    quarters.Add( quarter );
    quarter = quarter.GetPreviousQuarter();
  }

  return quarters;
} // GetPastQuarters