I am trying to improve some code and I can't think of a better way to do what it currently does. essentially i loop outside for the number of items divided by the number I want to select, then inside that loop I select the item based on an inner loop of the number of items I need to create a single item modifying that by the outerloop value, so in the example below the outer loop loops twice (0,1) and the inner loop twice per outer loop (0,1 then 2,3).
Can anyone see a more readable way to do this or a way to do it without doing the two loops?
Hope that makes sense. Thanks.
Consider XML of
string xml = @"
<MainItem>
<Select>2</Select>
<ItemArray>
<Item>One</Item>
<Item>Two</Item>
<Item>Three</Item>
<Item>Four</Item>
</ItemArray>
</MainItem>";
var doc = XDocument.Parse(xml);
Here the Select value tells me how many items create one "object" so in this case 2 items are an object (this can be any number but the number of items will always allow for the correct number of objects) currently i am doing somethng like
List<List<XElement>> items = new List<List<XElement>>();
for(int i = 0;i < (doc.Descendants("Item").Count() / (int)doc.Element("MainItem").Element("Select"));i++)
{
//this is one object
var singleItem = new List<XElement>();
for (int j = 0; j < (int)doc.Element("MainItem").Element("Select"); j++)
{
var item = doc.Descendants("Item").ElementAt(j + (i * (int)doc.Element("MainItem").Element("Select")));
singleItem.Add(item);
}
items.Add(singleItem);
}
So we end with a list of List with 2 items, each item holds 2 XElements each (One and Two together and Three and Four together) so..
ItemOne
One
Two
ItemTwo
Three
Four