Currently, it'll be IGrouping GetTopmostCategory(x)) ... public Category GetTopmostCategory(Item item) { Category category = item. Category; while (category. Parent!
= null) { category = category. Parent; } return category; } (You could put this into Category or Item potentially.) That would give you exactly the same return type, but the grouping would just be via the topmost category. Hope this is actually what you want.
Currently, it'll be IGrouping but if you want the topmost category to be the key, then the values could presumably be items or categories. You've shown the results as XML, but how are you actually going to use them? Given the results you've got, can't you easily get the parent category anyway?
Do you need to use the parent category in the actual grouping part? If two categories have the same parent category, do you want all the items in that parent category to be mashed together? Sorry for all the questions - but the more we know, the better we'll be able to help you.
EDIT: If you just want to group items by the topmost category, you can do items. GroupBy(x => GetTopmostCategory(x)) ... public Category GetTopmostCategory(Item item) { Category category = item. Category; while (category.
Parent! = null) { category = category. Parent; } return category; } (You could put this into Category or Item, potentially.
) That would give you exactly the same return type, but the grouping would just be via the topmost category. Hope this is actually what you want...
I used xml-ish just as an example, but I'm actually shoving the results of the GroupBy method into a ViewModel which will end up getting rendered to an MVC view page via a couple foreach loops (one for the group key and one for the group items). I can get the parent category with the results I have, but the rendering then gets a little messy trying to figure out if I've already displayed the parent category, etc. I'm open to other ideas... – mannish Jun 23 '09 at 16:22 And when I say "shoving the results... into a ViewModel", what I mean is a property on a ViewModel that is currently of type IEnumerable>. – mannish Jun 23 '09 at 16:23 So is that the type you actually want it to be?
Do you actually want to group by the top-most category for any item? If so, that's not too hard... although you'll still need to do a bit of work to render everything, I suspect. – Jon Skeet Jun 23 '09 at 16:28 Yes, I'd like to group by the top most category.
I figured I'd have to do a little bit of work, but I was hoping to leverage what Linq throws my way to alleviate some of that. – mannish Jun 23 '09 at 16:44 Editing answer... – Jon Skeet Jun 23 '09 at 16:45.
If you had a tree to walk, you'd already have items grouped by category. Do you control the interface of your view? Public abstract class TreeNode { private readonly int name; private Category c = null; public int Name { get { return name; } } public Category Parent { get { return c; } } public abstract string Tag { get; } public TreeNode(int n, Category c) { this.Name = n; AssignCategory(c); } public void AssignCategory(Category c) { if (c!
= null) { this. C = c; c. AddChild(this); } } public virtual IList Children { get { return null; } } } Item and Category look like public class Item : TreeNode { public Item(int n, Category c) : base(n, c) {} public override string Tag { get { return "Item"; } } } public class Category : TreeNode { List kids = new List(); public Category(int n, Category c) : base(n, c) {} public void AddChild(TreeNode child) { kids.
Add(child); } public override string Tag { get { return "Category"; } } public override IList Children { get { return kids; } } } Then you can show them with, say, a corny console display: public class CornyTextView { public int NodeDepth(TreeNode n) { if (n. Parent == null) return 0; else return 1 + NodeDepth(n. Parent); } public void Display(IEnumerable nodes) { foreach (var n in nodes.
OrderBy(n => n. Name)) { for (int I = 0; I c. Parent == null) .
Select(c => c as TreeNode)); } Notice that even though items is shuffled, the output is - Category 1 - Item 1 - Item 2 - Category 2 - Category 3 - Item 3 - Item 4 - Category 4 - Item 5 - Category 5 - Item 6.
This is definitely a workable solution and gets what I want in terms of display. But when I pull my date out of the db, my items and categories already have relationships, so rebuilding them into a Tree seems like an additional step. I just wanted to be able to group items hierarchically that are already related in an object graph.
– mannish Jun 23 '09 at 20:02.
Look at this blog. I like the Byerarchy extension method.
This is how I resolved my problem: var items = Items. GroupBy(x => x. Category).
GroupBy(y => y.Key. Parent); Which allowed me to do the following (nasty) rendering markup: Item Description Value I'd love to consolidate this tag soup, but I'll have to go with this for now.
I have a feeling that org.ccil.cowan. Tagsoup removes style attributes from td tag. Eg...Bill From NetFlow Analyzer after applying commandline.
Process it returns only Bill From NetFlow Analyzer.
Currently, it'll be IGrouping but if you want the topmost category to be the key, then the values could presumably be items or categories. You've shown the results as XML, but how are you actually going to use them? Given the results you've got, can't you easily get the parent category anyway?
Do you need to use the parent category in the actual grouping part? If two categories have the same parent category, do you want all the items in that parent category to be mashed together? Sorry for all the questions - but the more we know, the better we'll be able to help you.
(You could put this into Category or Item, potentially.) That would give you exactly the same return type, but the grouping would just be via the topmost category. Hope this is actually what you want...
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.