我使用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();
}
答案 0 :(得分:1)
如果集合的长度相同,则检查linq扩展名.Zip()。这将让您同时比较两者。例如:
var results = collection1.Zip(collection2, (a, b) => //check a against b
答案 1 :(得分:-1)
您的产品迭代在地址迭代中,因此可以首先遍历产品,然后迭代地址。您可以将变量更改为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个变量,而是并行地循环。
Br,Márton