ForEach Loop logic using KeyValuePairs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maylortaylor
    New Member
    • Nov 2012
    • 72

    ForEach Loop logic using KeyValuePairs

    I'm new to C#. I'm trying to create a program to audit some of our processors. As you will see below, I have two foreach loops that allow me access to errorOrders(ret urns <<UserName, #of errors>>) and totalOrders (returns <<UserName, #of totalOrders>>).

    However, the problem i'm running into now is that it seems to constantly loop through these two ForEach loops. When i run it, the 'count' on both errorOrders & totalOrders is 38. The program loops through all 38 users just fine, but then it continues to loop through users again...re-doing the process that it just finished.

    I'm looking to see if there is another way I could set this section up so that it only loops through users once. I was thinking about trying to create a 'Boolean flag' that would force the program out of the ForEach loop after it has run through all the users...but i'm sure there are other ways to handle this.

    Code:
    foreach (KeyValuePair<string, int> error in errorOrders)
    {
        foreach (KeyValuePair<string, int> total in totalOrders)
        {
    
            errPercentage = ((double)error.Value / (double)total.Value);                        
            Console.WriteLine("Percentage of errors for " + total.Key + ": " + Math.Round(errPercentage, 2) * 100 + "%");
            ordersPerHour = OrdersPerHour(total.Key);
            RandomOrders = RandomSelect(errPercentage, total.Key);
    
    
            Console.WriteLine("Number of orders pulled : " + RandomOrders.Rows.Count);
            //Print out orders randomly collected
            for (int i = 0; i < RandomOrders.Rows.Count; i++) 
            {
                Console.WriteLine(RandomOrders.Rows[i]["ControlNumber"]);
            }
    
            Console.WriteLine("\r\n");
            //NumOrdersToPull = FindNumOrdersToPull(Math.Round(errPercentage,2), ordersPerHour);
        }
    
    }
    I know i should probably seperate these two foreach loops instead of having them nested..however , the problem that arises once i do that is my errPercentage can not be calculated if i do.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I'm not sure I understand what you're trying to accomplish. What is the objective?

    Comment

    Working...