循环访问Multi key字典中的每个键

时间:2012-03-21 15:06:07

标签: c# dictionary

我已经编写了一个代码,用于为字典添加多个键,我按如下方式定义了字典并添加了键和值

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

现在我想按顺序循环遍历每个键以获取所需的项目,任何人都可以告诉我如何根据我的dictionary

为每个键值循环

我的代码

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;
}

4 个答案:

答案 0 :(得分:3)

按顺序遍历外部和内部

foreach (int key in outerDictionary.Keys.OrderBy(i=>i))
{
    // loop through the inner items by key
    var item = outerDictionary[key];
    foreach(int innerKey in item.Keys.OrderBy(i=>i))
    {
        ....
    }
}

答案 1 :(得分:2)

Dictionary<int, Dictionary<int, List<int>>> outer = new Dictionary<int, Dictionary<int, List<int>>>();
foreach(var outerKey in outer.Keys.OrderBy(x => x))
{
    foreach(var innerKey in outer[outerKey].Keys.OrderBy(x => x))
        Console.WriteLine(outer[outerKey][innerKey]);
}

答案 2 :(得分:0)

试试这个;

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

        foreach (var pair in outerDictionary)
        {
            foreach (var temp in pair.Value)
            {
                // do something
            }
        }

答案 3 :(得分:0)

这给了我更好的解决方案

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
          }
      }
   }