c#:如何循环访问 List<>,这是字典中的一个值

本文关键字:字典 一个 何循环 循环 List 访问 | 更新日期: 2023-09-27 18:37:23

现在我有一个字典,里面填充了一个帐户作为键,一个列表作为值。 我相信我的代码正在努力填充它。 但我的下一步是遍历与特定键关联的列表,并对列表执行操作(获取每个字段的总和)。 我对字典不太熟悉,所以我不确定如何访问值并执行操作。 我想做这个求和,并在退出 while 循环时将其打印在我的每个循环中。

1)如果我能弄清楚如何访问数据记录中的每个字段(在foreach循环内),我可能会弄清楚如何进行求和。

2)还在寻找一种打印值的方法,以便我查看它是否正确填充。

static void Main(string[] args)
{
    Dictionary<string, List<DataRecord>> vSummaryResults = new Dictionary<string, List<DataRecord>>();
    while (!r.EndOfStream)
    {
        if (control == "1")
        {
            // Need to add List<Datarecords> into dictionary...
            if (vSummaryResults.ContainsKey(records.account))
            {
                vSummaryResults[records.account].Add(records);
            }
            else
            {
                vSummaryResults.Add(records.account, new List<DataRecord>());
                vSummaryResults[records.account].Add(records);
            }
        }
   }
   foreach (List<DataRecord> rec in vSummaryResults.Values)
   {
       Console.WriteLine(rec.);  dictionary.
   }
   vWriteFile.Close();
   Console.ReadLine();
}

这是我用作列表中的对象的DataRecord类。

公共类数据记录{ 领域。。。。。}

c#:如何循环访问 List<>,这是字典中的一个值

对于字典的迭代,我们使用 KeyValuePair:

foreach (KeyValuePair<string, List<DataRecord>> kvp in vSummaryResults)
{
  string key = kvp.Key;
  List<DataRecord> list = kvp.Value;
  Console.WriteLine("Key = {0}, contains {1} values:", key, list.Count);
  foreach (DataRecord rec in list)
  {
     Console.WriteLine("  - Value = {0}", rec.ToString()); // or whatever you do to put list value on the output
  }
}

你需要在第一个循环中的另一个循环来获取列表中的值。

foreach (List<DataRecord> rec in vSummaryResults.Values)
{
    foreach(DataRecord data in rec)
    {
        Console.WriteLine(data .YourProperty);
    }
}

如果您知道密钥:

foreach (List<DataRecord> rec in vSummaryResults[key])
{
    foreach(DataRecord data in rec)
    {
        Console.WriteLine(data .YourProperty);
    }
}

我用一个简单的情况举了一个例子。它是一个字符串列表,但你可以做这个对象。ToString() 来获取一些 tekst;

要遍历键包含的列表,您需要再次迭代该列表,这就是第二个 foreach 所做的。

Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
List<String> ls = new List<string>();
ls.Add("item1");
ls.Add("item2");
dictionary.Add("it1", ls);
dictionary.Add("it2", ls);
foreach (var item in dictionary)
{
    foreach(var it in item.Value)
    {
        Console.WriteLine(it);
    }
}

你有字典

Dictionary<string, List<DataRecord>> vSummaryResults = new Dictionary<string, List<DataRecord>>();

你可以做一个

foreach (List<DataRecord> recs in vSummaryResults.Values)
{
    foreach (DataRecord rec in recs)
       Console.WriteLine(rec.Something); 
}

您尚未指定 DataRecord 的外观,但您将获得列表列表。可能有一个可爱的 Linq 表达式可以做同样的事情。

我从阅读您的代码中理解它,您有一个字典,其中每个键都有一个数据类型为DataRecord的列表。

使用电流回路

foreach (List<DataRecord> rec in vSummaryResults.Values)
    {
        Console.WriteLine(rec.);  dictionary.
    }

您循环遍历了数据类型为数据记录的每个列表。要访问存储在每个列表中的每个对象中的数据,您需要包含另一个 foreach 循环来循环访问列表中的对象。

 foreach (var rec in vSummaryResults.Values)
    {
        // At this point we have access to each individual List<DataRecord>
    foreach(var SingleDataRecordObj in rec)
     Console.WriteLine(ingleDataRecordObj.membervariablename)
      //From here we are able to list the individual variable member names      of values that are in the DataRecord data type, showing all of the information in each list that is stored in the values of the dictionary
    }

您不能修改 foreach 循环中的数据,但您可以为它们添加更简单的数据结构来执行您想要的任何换向等。

这是我需要做的代码。 按照我现在需要的方式工作,感谢大家的帮助。

    int iSumOpen = 0;
        foreach (KeyValuePair<string, List<DataRecord>> kvp in vSummaryResults)
        {
            string key = kvp.Key; //assigns key
            List<DataRecord> list = kvp.Value;  //assigns value
            Console.WriteLine("Key = {0}", key);
            iSumOpen = 0;
            foreach (DataRecord rec in list)
            {
                if (vSummaryResults.ContainsKey(key))
                {
                    iSumOpen += rec.open;
                }
                else
                {
                    vSummaryResults.Add(key, list);
                }
            }
            Console.WriteLine(iSumOpen);
        }