•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Nested Dictionary looping challenge need help!

nested dictionary

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

### #1 gaurigan

gaurigan

CC Lurker

• New Member
• 3 posts

Posted 09 December 2014 - 05:56 PM

when i add some keys and values to nested dictionary it (values ) turns up on all key values here is a sample code may be i am making some mistake??

static void Main(string[] args)
{
basictest();
}

//Nested dictionary problem
public static Dictionary<int, Dictionary<DateTime, String>> nestedict5 = new Dictionary<int, Dictionary<DateTime, string>>();

public static void basictest() {

int mk = 7001, nk = 7000; // just select 2 int values one odd(7001) and even(7000) for outer dictionary
DateTime ddn = DateTime.Now; // loop working with Datetime.Now for// inner dictionay
Dictionary<DateTime, string> cd = new Dictionary<DateTime, string>();
for (int i = 0; i < 12; i++)
{

if (ddn.Minute % 2 != 0) // if minute is odd
{
if (!(nestedict5[mk]).ContainsKey(ddn)) //check if outer dictionary odd key //contains this odd minute value
{

(nestedict5[mk]).Add(ddn, "odd"); // add odd string to inner with key of datetime,Minute odd
printvalues(mk); // check results of odd keys in debug windows
}
}
else if (ddn.Minute % 2 == 0) // if ninute is even
{
if (!(nestedict5[nk]).ContainsKey(ddn)) //check if even outer dictionay key //contains this even minute value
{

(nestedict5[nk]).Add(ddn, "even"); // add even string to inner with //key of Minute even
printvalues(nk); // check results of even keys in debug windows
}
}
}
}

// loop to print odd and even key values in debug window
public static void printvalues(int k)
{
var cc = (nestedict5[k]).Values.ToArray();
var ck = (nestedict5[k]).Keys.ToArray();
for (int i = 0; i < cc.Count(); i++)
{
DateTime dt = ck[i];
String dcandle = cc[i];
Debug.WriteLine(string.Format("pids1:{0},Count{1}, datetime:{2}: ticks:{3}", k * 100 + i, cc.Count(), ck[i], cc[i]));

}

}
Question is at the end of the loop i shall get 6 members in both odd and even minute
whereas due to some error i am getting 12 members in all/both odd and even key outer dictionary help

Here is output in debug windows
pids1:700100,Count1, datetime:12/10/2014 6:39:07 AM: ticks:odd ok

even key results:
pids1:700000,Count2, datetime:12/10/2014 6:39:07 AM: ticks:odd not ok
pids1:700001,Count2, datetime:12/10/2014 6:40:07 AM: ticks:even ok

odd key results:
pids1:700100,Count3, datetime:12/10/2014 6:39:07 AM: ticks:odd ok
pids1:700101,Count3, datetime:12/10/2014 6:40:07 AM: ticks:even not ok
pids1:700102,Count3, datetime:12/10/2014 6:41:07 AM: ticks:odd ok

The problem continue's so on till all loops as odd and even keys appear in both key values!?? which is not desired and only odd key should have odd values and even key even values

pids1:700000,Count4, datetime:12/10/2014 6:39:07 AM: ticks:odd
pids1:700001,Count4, datetime:12/10/2014 6:40:07 AM: ticks:even
pids1:700002,Count4, datetime:12/10/2014 6:41:07 AM: ticks:odd
pids1:700003,Count4, datetime:12/10/2014 6:42:07 AM: ticks:even

### #2 gaurigan

gaurigan

CC Lurker

• New Member
• 3 posts

Posted 09 December 2014 - 07:18 PM

namespace CollectionsTest
{
using System;
using System.Collections.Generic;
using System.Collections;//.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Runtime;
//  using Microsoft.FSharp.Collections;
using System.Collections.Concurrent;

class Program
{

static void Main(string[] args)
{
basictestc();
}
// loop to print odd and even key values in debug window
public static void printvalue(int k)
{
var cc = (Cnestedict5[k]).Values.ToArray();
var ck = (Cnestedict5[k]).Keys.ToArray();
for (int i = 0; i < cc.Count(); i++)
{
DateTime    dt = ck[i];
String  dcandle = cc[i];
Debug.WriteLine(string.Format("pids1:{0},Count{1}, datetime:{2}: ticks:{3}", k * 100 + i, cc.Count(), ck[i], cc[i]));

}

}

public static ConcurrentDictionary<int, ConcurrentDictionary<DateTime, String>> Cnestedict5 = new ConcurrentDictionary<int, ConcurrentDictionary<DateTime, string>>();
public static void basictestc()
{

int mk = 7001, nk = 7000;
DateTime ddn = DateTime.Now;
ConcurrentDictionary<DateTime, string> cd = new ConcurrentDictionary<DateTime, string>();
for (int i = 0; i < 12; i++)
{
if (ddn.Minute % 2 != 0)
{
printvalue(mk);
}
else if (ddn.Minute % 2 == 0)
{
printvalue(nk);
}
}
}

### #3 BlackRabbit

BlackRabbit

CodeCall Legend

• Expert Member
• 3871 posts

Posted 10 December 2014 - 11:48 AM

The problem lies here:

```            Dictionary<DateTime, string> cd = new Dictionary<DateTime, string>();

// you were using only one dictionary for both nestings, use a new one and it will work
Dictionary<DateTime, string> xd = new Dictionary<DateTime, string>();

// here, use the new one instead of cd
nestedict5.Add(nk, xd); //adding a new odd and //even dictionary to outer dictionary

```

### #4 gaurigan

gaurigan

CC Lurker

• New Member
• 3 posts

Posted 11 December 2014 - 03:15 AM

Thanks for your help and support

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download