我想在列表中合并所有子项目时寻求帮助,
public class Subitem
{
public string Name { get; set; }
public string Code { get; set; }
public float Price { get; set; }
}
public class Item
{
public string Name { get; set; }
public string Code { get; set; }
public List<Subitem> Subitems { get; set; }
}
var components = new List<Item>();
components.Add(new Item()
{
Code = "ItemCode1",
Name = "Item1Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode1", Price = 32 },
new Subitem { Code = "SubitemCode2", Price = 21 },
new Subitem { Code = "SubitemCode3", Price = 11 },
new Subitem { Code = "SubitemCode4", Price = 51 }
}
});
components.Add(new Item()
{
Code = "ItemCode2",
Name = "Item2Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode5", Price = 11 },
new Subitem { Code = "SubitemCode6", Price = 22 },
new Subitem { Code = "SubitemCode7", Price = 52 },
new Subitem { Code = "SubitemCode8", Price = 63 }
}
});
components.Add(new Item()
{
Code = "ItemCode3",
Name = "Item3Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode9", Price = 11 },
new Subitem { Code = "SubitemCode10", Price = 22 },
new Subitem { Code = "SubitemCode11", Price = 52 },
new Subitem { Code = "SubitemCode12", Price = 63 }
}
});
components.Add(new Item()
{
Code = "ItemCode4",
Name = "Item4Name",
Subitems = new List<Subitem>
{
new Subitem { Code = "SubitemCode13", Price = 11 },
new Subitem { Code = "SubitemCode14", Price = 22 },
new Subitem { Code = "SubitemCode15", Price = 52 },
new Subitem { Code = "SubitemCode16", Price = 63 }
}
});
我想在模型中组合所有子项,如下所示:
new { Code = SubitemCode1, Price = 32 }
...
new { Code = SubitemCode8, Price = 63 }
new { Code = "SubitemCode1:SubitemCode5", Price = 43 } //11 + 32
...
new { Code = "SubitemCode1:SubitemCode8", Price = 95 } //32 + 63
new { Code = "SubitemCode2:SubitemCode5", Price = ... }
...
new { Code = "SubitemCode2:SubitemCode8", Price = ... }
@EDIT
new { Code = "SubitemCode1:SubitemCode5:SubitemCode9", Price = 54 } // 11 + 32 + 11
...
new { Code = "SubitemCode1:SubitemCode5:SubitemCode12", Price = 96 } // 11 + 32 + 63
new { Code = "SubitemCode1:SubitemCode6:SubitemCode9", Price = ... }
...
new { Code = "SubitemCode1:SubitemCode6:SubitemCode12", Price = ... }
...
new { Code = "SubitemCode1:SubitemCode8:SubitemCode9", Price = ... }
...
new { Code = "SubitemCode1:SubitemCode8:SubitemCode12", Price = ... }
new { Code = "SubitemCode2:SubitemCode5:SubitemCode9", Price = ... }
...
有人可以向我解释如何继续吗?每个项目中的子项目中可能有1-5个项目和1-10个,我需要将所有子项目组合在一起并添加价格。
子项目中的子项目是不可组合的,只有其他项目中的子项目 >
先谢谢您
最好的问候。
答案 0 :(得分:1)
您可以简单地通过
这样的两个不同列表进行联接var result = components[0].Subitems
.Join(components[1].Subitems, x => true, y => true, (a, b) => new { Code = a.Code + ":" + b.Code, Price = a.Price + b.Price })
.ToList();
OR ,您可以使用linq
var result = from a in components[0].Subitems
from b in components[1].Subitems
select new
{
Code = a.Code + ":" + b.Code,
Price = a.Price + b.Price
};
最后打印结果
foreach (var item in result)
{
Console.WriteLine("Code: " + item.Code + "\t Price: " + item.Price);
}
输出:
答案 1 :(得分:0)
好的,我想我明白了。
代码如下:
var result = new List<SubItem>(); //list of combined SubItems
var ms = 0; // start index of items from list to combine
var mk = 0; // end index of items from list to combine
for (int i = 0; i < components.Count; i++) // count of all items
{
if (i == 0) //if there is a first item then we don't combine codes and prices
{
for (int mat = 0; mat < components[i].SubItems.Count; mat++)
{
var data = components[i].SubItem[mat];
result.Add(new SubItem { Price = data.Price, Code = data.Code });
mk = mat; // set last index of SubItem to combine
}
continue;
}
for (int j = ms; j < mk + 1; j++) // iterate from first to last SubItem to combine them with new SubItems
{
for (int mat = 0; mat < components[i].SubItems.Count; mat++) // iterate through SubItems
{
result.Add(new SubItem { Code = result[j].Code + ":" + components[i].SubItem[mat].Code, price = result[j].Price + components[i].SubItem[mat].Price }); // Combine last SubItem with now iterating Subitem.
}
}
ms = mk + 1; // update new start index to combine
mk = result.Count - 1; // update new end index to combine
}
我实现了自己想要的。 :)
最好的问候!