Foreach执行冗余逻辑

时间:2013-06-03 17:40:02

标签: c# foreach keyvaluepair

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

我的代码不断循环遍历这两个ForEaches。 '计数'在errorOrderstotalOrders都是38,程序循环遍历所有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);
    }

}

5 个答案:

答案 0 :(得分:6)

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

// (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
}

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

答案 1 :(得分:1)

对于总订单中的每个总计,第二个循环将运行一次。如果你想要它们只经过一次,不要嵌套它们。相反,让他们一个接一个。

foreach(total in total orders)
{

}
foreach(error in errororders)
{

}

如果它们是相关的,你只需要嵌套它们,你需要为外循环的每次迭代做一次内循环。

答案 2 :(得分: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);
    }
}

答案 3 :(得分: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
    }
}

答案 4 :(得分: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;
}