Reputation: 685
i want to ask you for help with combine all subitems from list, which looks like:
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 }
}
});
I want to combine all subitems in model which looks like this:
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 = ... }
...
Can anyone explain to me how to get on with it? There could be 1-5 Item and 1-10 in Subitems in each Item and i need to have all combination of subitems with the addition of prices.
Subitems from Item in which they are, is not combinable, only Subitems from other Item
Thank You in Advance,
Best Regards.
Upvotes: 0
Views: 373
Reputation: 685
Okay, i think that i got this.
The code looks like:
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
}
I achieved what I wanted. :)
Best Regards!
Upvotes: 0
Reputation: 9771
You can simply make join over two different list like
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 you can do this by using 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
};
And finally print the result
foreach (var item in result)
{
Console.WriteLine("Code: " + item.Code + "\t Price: " + item.Price);
}
Output:
Upvotes: 1