Reputation: 42
I get an array of Nodes and a Value into my Method which have to be nested into a Dictionary Structure. But I really need help with the logic here.
I did a static Test which shows what I need:
public static Dictionary<string, object> CreateNestedDictionary(string[] chainNodes, string value)
{
// Testvalues:
// ["application", "deepNode", "evenDeeperNode"], "Value"
var container = new Dictionary<string, object>();
container.Add("application", new Dictionary<string, object>() { { "deepNode", new Dictionary<string, string>(){{"evenDeeperNode", "Value" } }}});
return container;
}
So basically, I have a Dictionary<string, object>
variable, which can contain an infinite number of Dictionaries of the same type. At the very end, I need a Dictionary<string, string>
to define the last node and the value.
By Node I mean the Key of the Dictionary. Those will later be represented in a JSON Sctructure.
The thing is, that I don't know how many Elements I get in chainNodes
. Also, I may have to call this Method several times and add more Nodes. I would then have several chainNodes which have to go into the same Dictionary structure. Iwould then have to make sure that the Keys stay unique.
Is there any easy way to populate this structure dynamically? Or even a LINQ approach that deals with this? Any advice is much appreciated. Thank you in advance
After the comments I realized I did a really bad job explaining this. Here is another example:
public static Dictionary<string, object> CreateNestedDictionary(string[] chainNodes, string value)
{
// Testvalues:
// ["application", "deepNode", "evenDeeperNode"], "Value"
var container = new Dictionary<string, object>();
for (int i = 0; i < chainNodes.Length; i++)
{
if (i != chainNodes.Length -1)
{
container.Add(chainNodes[i], new Dictionary<string, object>()); // fill this empty Dictionary
}
else
{
container.Add(chainNodes[i], value);
}
}
}
Adding the last Dictionary is easy but I don't know how to fill the Dictionaries after the first.
Upvotes: 0
Views: 3710
Reputation: 11
Here's how I implemented infinite nesting Dictionaries.
My structure is: Dict([bool],object)
, ending with [bool] = true
.
public void Init(bool[] boolArray)
{
BoolDict = new Dictionary<bool, object>();
Dictionary<bool, object> parent = BoolDict;
for (int index = 0; index < boolArray.Length; index++)
{
if (index < boolArray.Length - 1)
{
Dictionary<bool, object> nestedDict = new Dictionary<bool, object>();
parent.Add(boolArray[index], nestedDict);
parent = nestedDict;
}
else
{
parent.Add(boolArray[index], true);
}
}
}
It's important to save the new Dict as the parent so that you can add to the new parent which you created in the previous iteration.
Upvotes: 1
Reputation: 108975
If I have understood this correctly: chainNodes
is a list of the names, and value
goes in the deepest dictionary (with name chainNodes[chainNodes.Length-1]
).
Summary: you need to iterate and special case the deepest node.
Something recursive is probably easiest, starting with the pure creation case (no need to check for existing entries).
public Dictionary<string, object> CreateNestedDictionary(string[] chainNodes, object value) {
return DoCreate(chainNodes, value, 0);
}
private Dictionary<string, object> DoCreate(string[] chainNodes, object value, int index) {
var res = new Dictionary<string, object>();
if (index == chainNodes.Length - 1) {
// Bottom out the recursion
res.Add(chainNodes[index], value);
} else {
res.Add(chainNodes[index], DoCreate(chainNodes, value, index+1));
}
return res;
}
Recursion naturally allows you to special case that last string because that is the end case that recursion must have.
For the adding to an existing collection case, you need to change the recursion to:
chainNodes[index]
key exists and use that entry or create a new one.Upvotes: 1
Reputation: 935
You could remove the first value from the chainNodes
and recursively call the method if there are any more values until you reach the end.
I'm with @Blorgbeard, though in that, this is a poor structure.
public static Dictionary<string, object> CreateNestedDictionary(string[] chainNodes, string value)
{
// Testvalues:
// ["application", "deepNode", "evenDeeperNode"], "Value"
var key = chainNodes.First();
chainNodes = chainNodes.Skip(1).ToArray();
return new Dictionary<string, object> { { key, chainNodes.Any() ? CreateNestedDictionary(chainNodes, value) : value } };
}
Upvotes: 0