【问题标题】:Looping through 2 collections concurrently C#同时循环遍历 2 个集合 C#
【发布时间】:2015-07-17 17:07:42
【问题描述】:

我使用 LINQ to XML 解析了两个保存客户订单信息的不同 XML 文件,并将集合对象传递给另一个函数,以同时遍历每个集合并将信息写入 excel 文件。我删除了一些不必要的代码,但基本上它是第一个集合地址上的 foreach 循环,然后是 products 集合上的嵌套 foreach 循环;但是,这不是我想要的,因为产品集合上的 foreach 循环不会与地址集合上的 foreach 循环同时运行,而是在移动到地址集合中的下一个位置之前迭代整个产品集合。我该如何解决这个问题?

    private static void WritetoExcel(IEnumerable<Address> addresses, IEnumerable<Address> products)
{
 foreach (var address in addresses)
               {

                    AddressLine1.Value2 = address.Name.ToUpper();
                    string a = WebUtility.HtmlDecode(address.AddressLine1);
                    AddressLine2.Value2 = a.ToUpper();
                    invoicenum.Value2 = "M" + num;
                    num++;

                    foreach(var product in products)
                    {
                        if (product.Title.Equals(sodiumChlorate))
                        {
                            UN.Value2 = "UN 1495, SODIUM CHLORATE";
                            HazardCode.Value2 = "3, PG II";
                            Package.Value2 = "500 GRAM BOTTLE";
                            FullName.Value2 = "SODIUM CHLORATE , ANALYZED ACS, REAGENT GRADE, CRYSTAL";
                            Box.Value2 = "PACKAGED ONE 500 GRAM BOTTLE PER BOX";
                            weight.Value2 = "12";
                            unitprice.Value2 = product.ItemPrice;
                            quantity.Value2 = product.Quantity;
                            unitship.Value2 = product.ShippingPrice;
                            totalship.Value2 = product.ShippingPrice;
                            decimal operand1 = decimal.Parse(product.ItemPrice);
                            decimal operand2 = int.Parse(product.Quantity);
                            total.Value2 = decimal.Multiply(operand1, operand2);
                        }
                        if (product.Title.Equals(semiNitric))
                        {
                            UN.Value2 = "UN2031, NITRIC ACID";
                            HazardCode.Value2 = "8, (5), PG II";
                            Package.Value2 = "2.5L POLY BOTTLE";
                            FullName.Value2 = "NITRIC ACID 70%, SEMICONDUCTOR, ELECTRONIC GRADE";
                            Box.Value2 = "PACKAGED 2.5L POLY BOTTLE PER BOX";
                            weight.Value2 = "12";
                            unitprice.Value2 = product.ItemPrice;
                            quantity.Value2 = product.Quantity;
                            unitship.Value2 = product.ShippingPrice;
                            totalship.Value2 = product.ShippingPrice;
                            decimal operand1 = decimal.Parse(product.ItemPrice);
                            decimal operand2 = int.Parse(product.Quantity);
                            total.Value2 = decimal.Multiply(operand1,operand2);
                       }
                        if (product.Title.Equals(acsNitric))
                        {
                            UN.Value2 = "UN2031, NITRIC ACID";
                            HazardCode.Value2 = "8, (5), PG II";
                            Package.Value2 = "2.5L POLY BOTTLE";
                            FullName.Value2 = "NITRIC ACID 70%, ACS, REAGENT GRADE";
                            Box.Value2 = "PACKAGED 2.5L POLY BOTTLE PER BOX";
                            weight.Value2 = "12";
                            unitprice.Value2 = product.ItemPrice;
                            quantity.Value2 = product.Quantity;
                            totalship.Value2 = product.ShippingPrice;
                            unitship.Value2 = product.ShippingPrice;
                            decimal operand1 = decimal.Parse(product.ItemPrice);
                            decimal operand2 = int.Parse(product.Quantity);
                            total.Value2 = decimal.Multiply(operand1, operand2);
                        }
                        if (product.Title.Equals(isopropyl))
                        {
                            UN.Value2 = "UN1219, ISOPROPANAOL";
                            HazardCode.Value2 = "3, PG II";
                            Package.Value2 = "QUART POLY BOTTLE";
                            FullName.Value2 = "ISOPROPYL ALCOHOL 99%, TECHNICAL GRADE";
                            Box.Value2 = "PACKAGED 4 X 1 POLY BOTTLE PER BOX";
                            weight.Value2 = "9";
                            unitprice.Value2 = product.ItemPrice;
                            quantity.Value2 = product.Quantity;
                            unitship.Value2 = product.ShippingPrice;
                            totalship.Value2 = product.ShippingPrice;
                            decimal operand1 = decimal.Parse(product.ItemPrice);
                            decimal operand2 = int.Parse(product.Quantity);
                            total.Value2 = decimal.Multiply(operand1, operand2);



                        }
                        if (product.Title.Equals(hazNitric))
                        {
                            UN.Value2 = "UN2031, NITRIC ACID";
                            HazardCode.Value2 = "8, (5), PG II";
                            Package.Value2 = "2.5L POLY BOTTLE";
                            FullName.Value2 = "NITRIC ACID 70%, ACS, REAGENT GRADE";
                            Box.Value2 = "PACKAGED 4 X 2.5L POLY BOTTLE PER BOX";
                            weight.Value2 = "33";
                            unitprice.Value2 = product.ItemPrice;
                            quantity.Value2 = product.Quantity;
                            unitship.Value2 = product.ShippingPrice;
                            totalship.Value2 = product.ShippingPrice;
                            decimal operand1 = decimal.Parse(product.ItemPrice);
                            int operand2 = int.Parse(product.Quantity);
                            total.Value2 = decimal.Multiply(operand1, operand2);
                        }
                    }




                   if (address.AddressLine2 != null)
                    {

                        AddressLine3.Value2 = address.AddressLine2.ToUpper();
                        AddressLine4.Value2 = address.City.ToUpper() + ", " + address.State.ToUpper();

                    }

                    else
                    {
                        AddressLine3.Value2 = address.City.ToUpper() + ", " + address.State.ToUpper();
                        AddressLine4.Value2 = "";
                    }

                    FinalShipment.Value2 = "FINAL SHIPMENT TO " + address.City.ToUpper() + ", " + address.State.ToUpper();
}

【问题讨论】:

  • 使用任务、线程或 PLINQ。选择一个。
  • 我会逐一阅读,谢谢

标签: c# linq loops foreach


【解决方案1】:

如果集合的长度相同,则检查 linq 扩展名 .Zip()。这将让您同时比较两者。示例:

var results = collection1.Zip(collection2, (a, b) => //check a against b

【讨论】:

  • 如何将其合并到我的代码中?我将 foreach 循环更改为:foreach (var address in addresses.Zip(products, (a, b) =&gt; new {a, b})) 但我在这部分的代码中遇到此错误AddressLine1.Value2 = address.Name.ToUpper(); 说:“匿名类型 #1”不包含“名称”的定义,也没有扩展方法“名称” " 可以找到接受“AnonymousType#1”类型的第一个参数。
  • 我想我是用AdressLine1.value2 = address.a.Name.ToUpper();弄明白的
【解决方案2】:

您的产品迭代在地址迭代中,因此先迭代产品然后再迭代地址可以正常工作。我猜您可以将变量更改为 IList。

private static void WritetoExcel(IList<Address> addresses, IList<Address> products)

然后你可以用相同的for循环遍历这两个变量。

for (int i = 0; i < addresses.Count; i++) {
    // do something with addresses[i] and products[i]

这样你循环遍历 2 个变量不是同时,而是并行。

兄弟,马顿

【讨论】:

  • 我真的很想在不将变量更改为 IList 并使用 for 循环的情况下完成此操作。
  • 您仍然可以使用 Zip 答案。接受它作为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-16
  • 2018-06-16
  • 2016-07-16
  • 2014-08-13
  • 1970-01-01
  • 2012-04-14
  • 2022-01-27
相关资源
最近更新 更多