同时循环两个集合c#

本文关键字:两个 集合 循环 | 更新日期: 2023-09-27 18:03:27

我通过使用LINQ to XML解析了保存客户订单信息的两个不同的XML文件,并将集合对象传递给另一个函数,以并发地遍历每个集合并将信息写入excel文件。我删除了一些不必要的代码,但基本上它是在第一个集合地址上的foreach循环,然后在产品集合上嵌套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();
}

同时循环两个集合c#

如果集合长度相同,则签出linq扩展名. zip()。这将让您同时比较两者。例子:

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

您的产品迭代在地址迭代中,因此首先遍历产品,然后遍历地址是可以的。我猜你可以把变量改成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]

这样你不是并发地,而是并行地遍历两个变量。

Br, Marton