如何在for循环中迭代日期?

Moh*_*Lal 6 c# for-loop

我有一些价值观.

 DateTime date=04/03/2015(date)
 Total Woring days=6 (total)
 Rotation Days=2 (rotationday)
 Shift Group=S1(this group contain two shift id 1 and 2)
Run Code Online (Sandbox Code Playgroud)

我想要轮班6天.但是每隔2天后,移位id 1旋转移动id 2,两天后再移动id 2旋转移位id 1等等......我的输出应该是

04/03/2015=1
05/03/2015=1
06/03/2015=2
07/03/2015=2
08/03/2015=1
09/03/2015=1
Run Code Online (Sandbox Code Playgroud)

我通过foreach循环获得shift id.我尝试了下面提到的方式但没有得到适当的结果.请帮我解决这个问题

SqlCommand cmd2 = new SqlCommand("select ShiftID from ShiftGroup  where  
ShiftName='" + ide + "'", conn2);
SqlDataAdapter sda2 = new SqlDataAdapter(cmd2);
DataSet ds4 = new DataSet();
var rows2 = ds4.Tables[0].Rows;
if (ds4.Tables[0].Rows.Count > 0)
{
foreach (DataRow row2 in rows2)
{
string shiftname = Convert.ToString(row2["ShiftID"]);
DateTime date=04/03/2015(date)
Total Woring days=6 (total)
Rotation Days=2 (rotationday)
DateTime rotation= date.AddDays(rotationday);//
DateTime totaldate = date.AddDays(workingdays);
for (DateTime rotation = date; rotation <= totaldate; rotation = rotation.AddDays(1))//total working days 
{
    //to check rotation date
if (rotation <= totaldate )
{
allocation.shiftallocation( rotation, shiftid);
}
}
}
Run Code Online (Sandbox Code Playgroud)

从最后一天开始我就被这个问题所困扰,任何人都帮助我.不需要考虑我的for循环,请提供一个生成上述输出的for循环

Mah*_*esh 3

你可以试试这个,

     DateTime date=DateTime.ParseExact("04/03/2015","dd/MM/yyyy",CultureInfo.InvariantCulture);
    DateTime totaldate = date.AddDays(6);
    for (int i=0; date <= totaldate; date = date.AddDays(1),i++)//total working days 
     {

         if((i/2)%2==0)
              Console.WriteLine(date+" "+1);
         else
              Console.WriteLine(date+" "+2);
     }
Run Code Online (Sandbox Code Playgroud)