lasta
lasta

Reputation: 311

Correct passage through the list of dates in the foreach loop

I have a problem in my code, where every time I start with the same date from the date list, I set the process to stop the foreach loop when it was done for the first time, but my way (for the second day I should take it in "arr" between dates) starts again from the first date. How can I get it in the right way? This is my code:

    #region Präsenzzeit-Check
        var arrivalDates = dataAccess.Get_dates(userID, date, DateTime.DaysInMonth(date.Year, date.Month)); //get dates for user
        var arrival = DateTime.Now;
        A_Zeitplan presences =null;
        int processed = 0;
        foreach (var arr in arrivalDates)
        {
            var arrivalDate = dataAccess.CheckPraesenzzeit(userID, arr);
            arrival = arrivalDate.Select(x => x.ZPZ_Bis).Where(x => x != null).LastOrDefault();
            foreach (var pr in presenceBlock)
            {
                presences = pr;
            }
            if (++processed == 1) break;
        }


        errorMessages.Add(error);
        #endregion

Upvotes: 0

Views: 380

Answers (1)

Caius Jard
Caius Jard

Reputation: 74660

Looking at your code, I'm not sure what the second foreach loop is for at all now, and I also believe that the first foreach loop runs only once (in which case it's not really a loop at all, just a code block)

I presume that rather than breaking, you need to perform some method call and process your items in your loop, one by one. Alternatively, put them in a modifiable collection, remove the first one, process it and then leave the remaining ones so that next time this code runs, it first item is a different one. This concept / container is called a Queue - a collection where items are added to the end and processed from the start (first in, first out)

I don't have any specific recommendations for how to modify this code snip to implement these because it's a very small snippet with no surrounding context, and it's not immediately obvious how it integrates into your wider program. The list of dates is fetched each time, which will overwrite the "just take the first" style I propose. Perhaps you should do something elsewhere so that get_dates doesn't return the same first date any more (mark it as processed somehow and exclude it from consideration)

As such, in a pseudocode point of view I think you need to look at a code structure like:

var collection = GetData(); //only return unprocessed items
foreach(item in collection)
  ProcessItemAndMarkAsCompleted(item)

Where you process all items in a loop and then move on with your program, or you need to queue things up and process them:

_classLevelQueueCollection.AddAllToEnd(GetData()); //GetData might return between 0 and 10 items
var item = _classLevelQueueCollection.Pop(); //remove one item from queue front
ProcessItemAndMarkAsCompleted(item);

In the second way the only possibility that the queue will get shorter is if GetData() often returns 0 items - if on average it always returns more than 1 item, your queue will get longer and longer.

This would be inefficient, but would work:

var collection = GetData(); //GetData returns all unprocessed items
var item = collection[0]; //get the first
ProcessItemAndMarkAsCompleted(item); //when marked as completed that item will not be returned by GetData() next time

Upvotes: 2

Related Questions