Answered by:
Storing data in an array

Question
-
Hi.
I have a datatable containing data in the following format:
Father1 - Son1 - child1
Father1 - Son2 - child1
Father1 - Son2 - child2
Father1 - Son3 - child1
Father1 - Son1 - child1
Father2 - Son1 - child1
..etc..
I will be populating a treeview control so need to store the data to show that there is one father, who can have many sons, and each one of those sons can in turn have many children.
Can anyone offer any advice on how to loop through the table and store this data in an array, list or custom class?
Thank you.
Tuesday, September 25, 2012 1:02 PM
Answers
-
A dictionary with the father as key containing dictionaries with the son as key and the son's children as value:
Dictionary<string, Dictionary<string, List<string>>>
For each line of the datatable, check if you already have that father; if not, create a new Dictionary<string, List<string>>
Check if the son is in the subdictionary. If it is, add the child to the list; if it is not, create a new List<string>, add the child and add the list to the subdictionary.
- Marked as answer by DingleB3rry Tuesday, September 25, 2012 3:41 PM
Tuesday, September 25, 2012 3:36 PM -
Dictionary<string, Dictionary<string, List<string>>> companydictionary = new Dictionary<string, Dictionary<string, List<string>>>(); for (int i = 0; i < alist.Count; i++) { row = (DataRow)alist[i]; Dictionary<string, List<string>> productsdictionary; // check if you already have that father if (!companydictionary.TryGetValue(row[1].ToString(), out productsdictionary)) { // if not, create a new Dictionary<string, List<string>> productsdictionary = new Dictionary<string, List<string>>(); // add the subdictionary to the companydictionary companydictionary.Add(row[1].ToString(), productsdictionary); } List<string> children; // Check if the son is in the subdictionary if (!productsdictionary.TryGetValue(row[3].ToString(), out children)) { // if it is not, create a new List<string> children = new List<string>(); // add the list to the subdictionary productsdictionary.Add(row[3].ToString(), children); } // add the child to the list children.Add(row[4].ToString()); }
- Marked as answer by DingleB3rry Thursday, September 27, 2012 2:58 PM
Thursday, September 27, 2012 12:10 PM
All replies
-
See if this MSDN forum thread helps.
Sameer
If this answers your question, please Mark it as Answer. If this post is helpful, please vote as helpful.
- Proposed as answer by ArifMustafa Tuesday, September 25, 2012 1:19 PM
Tuesday, September 25, 2012 1:13 PM -
Not really, I dont understand what the code is doing. I would prefer to somehow structure the information before building the tree. in a custom class that contains an array or something..
Tuesday, September 25, 2012 3:26 PM -
A dictionary with the father as key containing dictionaries with the son as key and the son's children as value:
Dictionary<string, Dictionary<string, List<string>>>
For each line of the datatable, check if you already have that father; if not, create a new Dictionary<string, List<string>>
Check if the son is in the subdictionary. If it is, add the child to the list; if it is not, create a new List<string>, add the child and add the list to the subdictionary.
- Marked as answer by DingleB3rry Tuesday, September 25, 2012 3:41 PM
Tuesday, September 25, 2012 3:36 PM -
Thank you, that makes sense.Tuesday, September 25, 2012 3:42 PM
-
Hi there. I understand the logic behind your answer but I dont know how to implement it. Here is my code but the problem is that I dont know how to create a new instance of an object within a loop that is unique, and then add it to the dictionary while still in the loop!
private void button4_Click(object sender, EventArgs e)
{
string query = "select a.customercode, a.customername, b.productcode, b.productname, c.componentname, d.quantity " +
"FROM(((customer a INNER JOIN products b ON a.customercode = b.customercode) INNER JOIN prodcompjunction d ON " +
"b.productcode = d.productcode) INNER JOIN components c ON d.componentcode = c.componentcode) " +
"ORDER BY a.customercode, b.productcode";ArrayList alist = new ArrayList();
alist = DB.CreateArrayList(query);
//craete a row to extract from arraylist
DataRow row = (DataRow)alist[1];
Dictionary<string, Dictionary<string, List<string>>> companydictionary = new Dictionary<string, Dictionary<string, List<string>>>();
Dictionary<string, List<string>> productsdictionary = new Dictionary<string, List<string>>();
List<String> BOMList = new List<String>();
for (int i = 0; i < alist.Count; i++)
{
row = (DataRow)alist[i];
// See whether Dictionary contains this string.
if (!companydictionary.ContainsKey(row[1].ToString()))
{
companydictionary.Add(row[1].ToString(), productsdictionary);
}
if (!productsdictionary.ContainsKey(row[3].ToString()))
{
productsdictionary.Add(row[3].ToString(), BOMList);
}
BOMList.Add(row[4].ToString());
}
}Thursday, September 27, 2012 11:23 AM -
Dictionary<string, Dictionary<string, List<string>>> companydictionary = new Dictionary<string, Dictionary<string, List<string>>>(); for (int i = 0; i < alist.Count; i++) { row = (DataRow)alist[i]; Dictionary<string, List<string>> productsdictionary; // check if you already have that father if (!companydictionary.TryGetValue(row[1].ToString(), out productsdictionary)) { // if not, create a new Dictionary<string, List<string>> productsdictionary = new Dictionary<string, List<string>>(); // add the subdictionary to the companydictionary companydictionary.Add(row[1].ToString(), productsdictionary); } List<string> children; // Check if the son is in the subdictionary if (!productsdictionary.TryGetValue(row[3].ToString(), out children)) { // if it is not, create a new List<string> children = new List<string>(); // add the list to the subdictionary productsdictionary.Add(row[3].ToString(), children); } // add the child to the list children.Add(row[4].ToString()); }
- Marked as answer by DingleB3rry Thursday, September 27, 2012 2:58 PM
Thursday, September 27, 2012 12:10 PM -
Wow that is complicated! I put that in the debugger and it worked [THANK YOU], but Im still trying to wrap my head around it.
so whenever you declare a new 'list' object, the ones that have already been added to the dictionary are untouched. That simple misunderstanding is what was throwing me.
Thank you again!
Thursday, September 27, 2012 2:58 PM