473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ForEach Loop logic using KeyValuePairs

maylortaylor
72 New Member
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(returns <<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.

Expand|Select|Wrap|Line Numbers
  1. foreach (KeyValuePair<string, int> error in errorOrders)
  2. {
  3.     foreach (KeyValuePair<string, int> total in totalOrders)
  4.     {
  5.  
  6.         errPercentage = ((double)error.Value / (double)total.Value);                        
  7.         Console.WriteLine("Percentage of errors for " + total.Key + ": " + Math.Round(errPercentage, 2) * 100 + "%");
  8.         ordersPerHour = OrdersPerHour(total.Key);
  9.         RandomOrders = RandomSelect(errPercentage, total.Key);
  10.  
  11.  
  12.         Console.WriteLine("Number of orders pulled : " + RandomOrders.Rows.Count);
  13.         //Print out orders randomly collected
  14.         for (int i = 0; i < RandomOrders.Rows.Count; i++) 
  15.         {
  16.             Console.WriteLine(RandomOrders.Rows[i]["ControlNumber"]);
  17.         }
  18.  
  19.         Console.WriteLine("\r\n");
  20.         //NumOrdersToPull = FindNumOrdersToPull(Math.Round(errPercentage,2), ordersPerHour);
  21.     }
  22.  
  23. }
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.
Jun 3 '13 #1
1 3109
Rabbit
12,516 Recognized Expert Moderator MVP
I'm not sure I understand what you're trying to accomplish. What is the objective?
Jun 3 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mike P | last post by:
In a foreach loop, how do you break out of the iteration but not the loop? Break and continue both seem to exit the loop, I need something that for example on an if statement will cease execution...
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
18
by: Ken Varn | last post by:
Is there any way to reset a foreach loop to re-iterate through the collection as if it were starting from the beginning? Namely, if I delete an item out of a collection, I want to be able to reset...
3
by: rhaazy | last post by:
If I want to use a FOREACH loop on an array of strings, but didn't want to include the first member,how would I do this??? Psuedo Code::: foreach(string a in myStringArray (where a.index>0))...
3
by: cody | last post by:
Currently it is only legal to use types which has a method named GetEnumerator to be used in a foreach loop. This makes it impossible to use the same Enumerator after and before a foreach loop,...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
1
by: Perl Beginner | last post by:
I hope i can articulate this question properly. i have been trying to figure this out for over a week. I am comparing the contents of two files, but the comparison is done inside of a foreach...
3
by: SM | last post by:
Hello, I have an array that holds images path of cd covers. The array looks like this: $cd = array( 589=>'sylver.jpg', 782=>'bigone.jpg', 158=>'dime.jpg' );
15
by: fjm | last post by:
Hello everyone and happy Sunday. :) I have a problem that I *think* I may know the solution to but have no idea how to write the code for it. I am working on a templating system wher I have...
1
by: greyseal96 | last post by:
Hi, I am a pretty new programmer, so I apologize in andvance if this is a dumb question... In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.