0

我的两个 ForEach 循环允许访问errorOrders(用户名及其错误数)和totalOrders(用户名及其总订单数)。

我的代码不断循环通过这两个 ForEache。两者的“计数”都是 38 errorOrderstotalOrders程序循环遍历所有 38 个用户就好了。但随后它会一次又一次地循环遍历它们,重新执行刚刚完成的过程。

我怎样才能遍历用户一次?

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

}
4

5 回答 5

5

分开循环而不是嵌套它们。将一个放在另一个里面会使整个子循环为父循环的每个实例运行。(由此得名。)

// (shared variables here)

foreach (KeyValuePair<string, int> total in totalOrders)
{
    // Code relevant to all orders here
}

foreach (KeyValuePair<string, int> error in errorOrders)
{
    // Code relevant to erroneous orders only here
}

如果循环需要共享变量,请在第一个循环之前声明它们。在循环中创建的变量将是本地的,并且在它们的循环完成时停止存在。

于 2013-06-03T17:44:07.350 回答
1

第二个循环将针对总订单中的每个总数运行一次。如果您希望它们只经过一次,请不要嵌套它们。而是让他们一个接一个。

foreach(total in total orders)
{

}
foreach(error in errororders)
{

}

如果它们是相关的,您只需要嵌套它们,并且您需要为外部循环的每次迭代执行一次内部循环。

于 2013-06-03T17:48:58.450 回答
1

试试这个:

foreach (KeyValuePair<string, int> error in errorOrders)
{
    if (totalOrder.HasKey(error.Key) {
        var total = totalOrders[error.Key];

        errPercentage = ((double)error.Value / (double)total);                        
        Console.WriteLine("Percentage of errors for " + error.Key + ": " + Math.Round(errPercentage, 2) * 100 + "%");
        ordersPerHour = OrdersPerHour(error.Key);
        RandomOrders = RandomSelect(errPercentage, error.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);
    }
}
于 2013-06-03T17:54:00.680 回答
0

或者甚至只是一个循环,然后查找另一个?totalOrders 是某种列表吗?如果你能把一个变成字典,你可以做类似的事情

foreach (KeyValuePair<string, int> total in totalOrders)
{
    // do work for each order
    object whatever;
    if (errorOrders.TryGetValue( total.Key, out whatever ))
    {
        // do extra work because this order has an error
    }
}
于 2013-06-03T17:49:18.577 回答
0

其他答案帮助我得出了这个结论:

foreach (KeyValuePair<string, int> e in errorOrders)
{
    errPercentage = GetErrPercentage(e.Key);
    Console.WriteLine("Percentage of errors for " + e.Key + ": " + Math.Round(errPercentage, 2) * 100 + "%");
    ordersPerHour = OrdersPerHour(e.Key);
    RandomOrders = RandomSelect(errPercentage, e.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");

static double GetErrPercentage(string user)
{
    double errPercentage = 0;
    errPercentage = (double)errorOrders[user]/ (double)totalOrders[user];          
    return errPercentage;
}
于 2013-06-03T21:16:31.773 回答