I have an odd sorting case I'm struggling to work out using LINQs GroupBy method.
I have two classes: Category and Item. Every Item has a category, and a Category can have a parent Category. What I need to do is organize all of the Items by their proper Category, but I also want to sort the Categories by the parent Category if there is one.
So ideally I should be able to visualize my results like:
<Category 1>
<Item 1>
<Item 2>
</Category 1>
<Category 2>
<Category 3>
<Item 3>
<Item 4>
</Category 3>
</Category 2>
<Category 4>
<Item 5>
</Category 4>
<Category 5>
<Item 6>
</Category 5>
I'm currently using items.GroupBy(x => x.Category)
which gives me everything except the parent categories. So my results look like:
<Category 1>
<Item 1>
<Item 2>
</Category 1>
<Category 3>
<Item 3>
<Item 4>
</Category 3>
<Category 4>
<Item 5>
</Category 4>
<Category 5>
<Item 6>
</Category 5>
The issue being that (in this example) the parent category for Category 3 (Category 2) isn't listed.
I started goofing around with nested groups, but I didn't get very far before considering just manually walking the tree myself (foreach'ing). Before I do that, I'm hoping the LINQ gurus here can help me out...