2015-03-04 13 views
6

Ich habe einige Werte.Wie wiederhole ich das Datum in der for-Schleife?

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) 

Ich möchte die Schicht für 6 Tage ausführen. aber nach jedem 2 Tage Shift-ID 1 drehen zu verschieben ID 2 und nach zwei Tagen wieder verschieben id 2 drehen id 1 zu verschieben und so weiter ... sollte mein ausgegeben werden wie

04/03/2015=1 
05/03/2015=1 
06/03/2015=2 
07/03/2015=2 
08/03/2015=1 
09/03/2015=1 

Ich erhalte Verschiebung id durch eine foreach-Schleife. Ich habe versucht, wie unten erwähnt, aber keine richtige Ergebnisse zu bekommen. Bitte helfen Sie mir, dieses Problem zu lösen

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

Ich bin mit diesem vom letzten Tag hängen geblieben, jemand hilft mir. Keine Notwendigkeit von betrachten meiner for-Schleife, schicken Sie bitte eine for-Schleife, die die oben genannten Ausgabe

+0

Was ist 'rt'? Was ist das wert? –

+0

@Coder des Codes, bitte überprüfen Sie meine aktualisierte Frage bitte –

+0

Sollten Sie Urlaub in Betracht ziehen? – Sarathy

Antwort

3

Sie können dies versuchen,

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

Termine kommt als meine Ausgabe, aber Shiftid nicht rotiert sein nur 1 nicht zu 2 –

+0

drehen Aber das geht 1,2,1,2, usw. OP will 1 , 1,2,2,1,1,2,2 usw. –

+0

@Jim Mischel, ja du hast recht –

1
var date = DateTime.Parse("04/03/2015"); 

var totalWorkingDays = 6; 
var rotationDays = 2; 

var rotationId = 1; 
var rotationCounter = 1; 

for (DateTime rotation = date; 
    rotation <= date.AddDays(totalWorkingDays); 
    rotation = rotation.AddDays(1)) 
{ 
    Console.WriteLine(rotation + " " + rotationId); 

    if (rotationCounter++ == 2) 
    { 
     rotationCounter = 1; 
     rotationId = 3 - rotationId; 
    } 
} 

Oder mit einer Linq-Abfrage erzeugen

var date = DateTime.Parse("04/03/2015"); 

var totalWorkingDays = 6; 
var rotationDays = 2; 

foreach (var d in Enumerable.Range(0, totalWorkingDays) 
       .Select((i, index) => date.AddDays(i) + 
        (((int)(index/rotationDays)) % 2 == 1 ? " 2" : " 1"))) 
{ 
    Console.WriteLine(d); 
} 
1

Verwenden Sie keine Daten im Schleifenzyklus, verwenden Sie abstrakte Indizes. Sie können wirklich von konkreten Zahlen abstrahieren und nur Variablen zur Ergebnisverwaltung verwenden. So können Sie die Anzahl der rotationDays einfach ändern und sogar das Etiketten-Array verschieben, ohne den Zyklus zu ändern.

var date = DateTime.Parse("04/03/2015"); 

     var totalWorkingDays = 15; 
     var rotationDays = 2; 

     var workshifts = new[] { "S1", "S2" }; 
     var currentWorkshiftIndex = 0; 
     for (int dayIndex = 0; dayIndex <= totalWorkingDays; dayIndex++) { 
      if (dayIndex != 0 && dayIndex % rotationDays == 0) currentWorkshiftIndex = (currentWorkshiftIndex + 1) % workshifts.Length; 
      Console.WriteLine(date.AddDays(dayIndex) + " " + workshifts[currentWorkshiftIndex]); 
     } 
1

Eine generische Lösung mit Ferien berücksichtigt.

int totalDays = 10; 
int rotationDays = 3; 
int[] shifts = new[] { 1, 2 }; 
string startDate = "04/03/2015"; 
var holidays = new List<DayOfWeek>(new[] { DayOfWeek.Saturday, DayOfWeek.Sunday, DayOfWeek.Tuesday }); 
var shiftAllocations = new Dictionary<DateTime, int>(); 
int currentShiftIndex = 0; 

DateTime current = DateTime.ParseExact(startDate, "dd/MM/yyyy", CultureInfo.InvariantCulture); 

for (int i = 0; i < totalDays; i += rotationDays) 
{ 
    var currentShiftId = shifts[currentShiftIndex]; 

    // For each day from the current day till the number of shift rotation days, allocate the shift. 
    for (int j = 0; j < rotationDays;) 
    { 
     // If the current day is a holiday, move to the next day by incrementing i. 
     if (holidays.Contains(current.AddDays(i + j).DayOfWeek)) 
     { 
      i++;       
      continue; 
     } 

     shiftAllocations.Add(current.AddDays(i + j), currentShiftId); 
     j++; 
    } 

    // Increase the shift index if the value is within the bounds. If not reset the index to the beginning 
    if ((currentShiftIndex + 1) >= shifts.Length) 
     currentShiftIndex = 0; 
    else 
     currentShiftIndex++; 
} 

foreach (var kvp in shiftAllocations) 
{ 
    Console.WriteLine(kvp.Key.ToString("dd/MM/yyyy") + ":" + kvp.Value); 
} 
1

nach Ihren Kommentar für die 5 Arbeitstagen pro Woche schrieb ich folgendes:

var start = new DateTime(2015, 04, 03); 
    var working_day_count = 6; 
    var rotation_interval = 2; 
    var shifts = new List<int> { 1, 2 }; 

    var rotation_count = 1; 
    var shift_index = 0; 

    for (var i = 0; i < working_day_count; i++) { 
    while ((start.DayOfWeek == DayOfWeek.Saturday) || 
     (start.DayOfWeek == DayOfWeek.Sunday)) { 
     start = start.AddDays(1); 
    } 

    Console.WriteLine(start.ToString() + " = " + shifts[shift_index].ToString()); 

    start = start.AddDays(1); 

    rotation_count++; 
    if (rotation_count > rotation_interval) { 
     rotation_count = 1; 
     shift_index++; 
     if (shift_index >= shifts.Count) { 
     shift_index = 0; 
     } 
    } 
    } 

Nur die Werte der ersten vier varibales ändern, wie Sie möchten. Ich habe versucht, es einfach zu verstehen, anstatt performant oder kompakt.

Verwandte Themen