【发布时间】: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。选择一个。
-
我会逐一阅读,谢谢