4
Answers

Loop through for each key in Multi key dictionary

Photo of Dorababu Meka

Dorababu Meka

13y
2.2k
1
I have written a code for adding multiple keys for a dictionary for that I defined my dictionary as follows and added the keys and values

Dictionary<int, Dictionary<int, List<int>>> outerDictionary = new Dictionary<int, Dictionary<int, List<int>>>();

Now I would like to loop through each keys in-order to get the required items can any one tell how can I loop through for each key value as per my dictionary

My code

protected void Page_Load(object sender, EventArgs e)
    {
        Dictionary<int, Dictionary<int, List<int>>> outerDictionary = new Dictionary<int, Dictionary<int, List<int>>>();
        Dictionary<int, List<int>> innerDictionary;

        ArrayList arrPayPeriodID = new ArrayList();
        ArrayList arrPayYear = new ArrayList();
        ArrayList arrEmpID = new ArrayList();

        int[] ipayYear = new int[] { };
        int[] iPayPeriodID = new int[] { };
        int[] iEmpID = new int[] { };

        int ipayYr = 2011;
        int ipayYr1 = 2011;
        int ipayYr2 = 2012;
        int ipayYr3 = 2012;

        int PayPeriodID = 1;
        int payperiodid1 = 2;
        int payperiodid2 = 2;
        int payperiodid3 = 2;

        int EmpID = 1;
        int EmpID1 = 1;
        int EmpID2 = 1;
        int EmpID3 = 1;

        arrEmpID.Add(EmpID);
        arrEmpID.Add(EmpID1);
        arrEmpID.Add(EmpID2);
        arrEmpID.Add(EmpID3);

        arrPayPeriodID.Add(PayPeriodID);
        arrPayPeriodID.Add(payperiodid2);
        arrPayPeriodID.Add(payperiodid3);
        arrPayPeriodID.Add(payperiodid1);

        arrPayYear.Add(ipayYr);
        arrPayYear.Add(ipayYr1);
        arrPayYear.Add(ipayYr2);
        arrPayYear.Add(ipayYr3);

        iEmpID = (int[])arrEmpID.ToArray(typeof(int));

        iPayPeriodID = (int[])arrPayPeriodID.ToArray(typeof(int));

        ipayYear = (int[])arrPayYear.ToArray(typeof(int));


        DataTable table = GetTable(iEmpID, ipayYear, iPayPeriodID);
        DataRow row = table.Rows[0];

        for (int i = 0; i < table.Rows.Count; i++)
        {
            DataRow row1 = table.Rows[i];
            var employeeId = (int)row1["EmpID"];
            var payYear = (int)row1["Year"];
            var payId = (int)row1["PayID"];


            
            if (!outerDictionary.TryGetValue(employeeId, out innerDictionary))
            {
                innerDictionary = new Dictionary<int, List<int>>();
                outerDictionary.Add(employeeId, innerDictionary);
            }
            List<int> list;
            if (!innerDictionary.TryGetValue(payYear, out list))
            {
                list = new List<int>();
                innerDictionary.Add(payYear, list);
            }

            list.Add(payId);
        }
    }

    static DataTable GetTable(int[] empID, int[] payYr, int[] payID)
    {

        // Here we create a DataTable with four columns.
        DataTable table = new DataTable();
        table.Columns.Add("EmpID", typeof(int));
        table.Columns.Add("Year", typeof(int));
        table.Columns.Add("PayID", typeof(int));

        // Here we add five DataRows.
        for (int i = 0; i < empID.Length; i++)
        {
            table.Rows.Add(empID[i], payYr[i], payID[i]);
        }
        return table;
    }

Answers (4)

1
Photo of Vulpes
NA 98.3k 1.5m 13y
You should be able to do it as follows:

       foreach(int outerKey in outerDictionary.Keys)
       {
          Dictionary<int, List<int>> outerValue = outerDictionary[outerKey];
 
          foreach(int innerKey in outerValue.Keys)
          {
              List<int> innerValue = outerValue[innerKey];
              foreach(int item in innerValue)
              {
                 // do something with item
              }
          }
       } 
Accepted
0
Photo of Vulpes
NA 98.3k 1.5m 13y
OK, to iterate it now, the following should work.

As I'm not sure whether the two lists within each MyValue struct correspond to each other and therefore have the same number of items, I've iterated them separately for now:

       foreach(int outerKey in outerDictionary.Keys)
       {
          MyDictionary outerValue = outerDictionary[outerKey];
 
          foreach(int innerKey in outerValue.Keys)
          {              
              MyValue innerValue = outerValue[innerKey];

              foreach(int item in innerValue.lstPayPeriodID)
              {
                  // do something with item
              }

              foreach(DateTime item in innerValue.lstPaymentDate)
              {
                 // do something with item
              }            
          }
       } 


0
Photo of Dorababu Meka
NA 8.2k 1.2m 13y
Hi vulpes I am having an issue i.e for the first loop through first dictionary I am unable to get the Items for the first time

This is my code

Dictionary<int, MyDictionary> outerDictionary = new Dictionary<int, MyDictionary>();

This is how MyDictionary defined

public struct MyValue
    {
        public List<int> lstPayPeriodID;
        public List<DateTime> lstPaymentDate;
    }


    public class MyDictionary : Dictionary<int, MyValue>
    {
        public void Add(int key, List<int> lstpayID, List<DateTime> lstPaymntDate)
        {
            MyValue v1 = new MyValue();
            v1.lstPayPeriodID = lstpayID;
            v1.lstPaymentDate = lstPaymntDate;
            this.Add(key, v1);
        }
    }

This is how i added keys and values to the dictionary

for (int iempIdcnt = 0; iempIdcnt < EmpIDs.Length; iempIdcnt++)
        {
            if (!outerDictionary.TryGetValue(EmpIDs[iempIdcnt], out dictAddValues))
            {
                dictAddValues = new MyDictionary();
                outerDictionary.Add(EmpIDs[iempIdcnt], dictAddValues);
            }
        }


        for (int iDictCnt = 0; iDictCnt < iPayYear.Length; iDictCnt++)
        {
            if (!(dictAddValues.ContainsKey(iPayYear[iDictCnt])))
            {
                List<int> lstiPayperiodID = new List<int>();
                List<DateTime> lstiDateTime = new List<DateTime>();
                dictAddValues.Add(iPayYear[iDictCnt], lstiPayperiodID, lstiDateTime);
            }
            dictAddValues[iPayYear[iDictCnt]].lstPaymentDate.Add(dtPaymentDates[iDictCnt]);
            dictAddValues[iPayYear[iDictCnt]].lstPayPeriodID.Add(iPayPeriodID[iDictCnt]);
        }

As per your code I do this as follows

foreach (int outerKey in outerDictionary.Keys)
        {
            MyDictionary outerValue = outerDictionary[outerKey]; // Here I am not getting the values for first time..
            foreach (int innerKey in outerValue.Keys)
            {
                -------------------
                ------------------- My Code
                ------------------- 
             }
         }
           


0
Photo of Dorababu Meka
NA 8.2k 1.2m 13y

I expected the answer from you and you only replied, thanks for your quick answer vulpes.. You rock...