I have an IList<Category>
The Category type is coming from my Category Table in SQL Server:
Table: Category
CategoryID, ParentCategoryID
so typical heirarchy in one table.
So if I have this IList of Categories that contain the node relationships, then I am trying to figure out how this fits into making a Node and Tree like a lot of people are doing. And how would the constructor look and this class be created in terms of usage?
I think it makes sense here to create a generic Node class and Tree class so I can reuse in the future I think for other tree types.
So if I have something like this, how is T being used and what am I benefiting from?
(pseudo code here)
public class Node<T>
...
Node<T> _parentNode;
List<Node<T>> _children;
private void SetParentNode(T)
private void AddChild(T)
... etc.
trying to understand the concept here on why a Generic Node class would be used for any type coming in like Childeren, etc. that contains a child/parent relationship (int Ids)
UPDATE
So having an issue here with the GroupBy recommendation. Check out what I tried to do with your example:
First I have this property in my Tree class:
public Dictionary, IList>> ParentNodeAndRelatedChildrenFlattenedMap { get; private set; }
and incoming to my class's constructor is a IList dependencies that I converted (looped and created a new Node for every one of them) to a IList>
Now I'm trying to group that list by Node.ParentId as you were talking so that I get a grouping on Parent Nodes and since each node has its children property it's easy to find out what the related children are to those parent nodes.
But here is the problem now later down in my code:
public void CreateFlattenedMap()
{
var parentGroups = _nodeDependencies.GroupBy(d => d.ParentNodeId);
var dictionary = parentGroups.ToDictionary(d => d, d => d.ToList());
ParentNodeAndRelatedChildrenFlattenedMap = dictionary;
}
well it's not liking my assignment of dictionary because it's an > dictionary that's created by the ToDictionary(). So not sure how to get this grouping grouped and to a dictionary that is a , List> where Node in the dictionary is the Node instance for that parent I'm grouping on (yea I'm grouping on its Node.ParentId but I want the Node though in the ToDictionary in the end) and the List> is the list of Children Nodes from the Parent Node.Children property.