繁体   English   中英

c#:如何遍历List <>,这是字典中的值?

[英]c#: How to iterate through a List<>, that is a value in a dictionary?

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

1)如果我可以弄清楚如何访问DataRecords中的每个字段(在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();
}

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

公共类DataRecord {字段.....}

对于字典的迭代,我们使用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
  }
}

你有一本字典

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表达式可以做到这一点。

您需要在第一个循环内添加另一个循环以获取列表中的值。

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

我举了一个简单情况的例子。 它是一个字符串列表,但是您可以执行object.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);
    }
}

通过阅读代码可以理解这一点,您有一个字典,其中每个键都有一个DataRecord类型的列表。

随着您当前的循环

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

    }

您遍历了每个类型为DataRecord的List。 要访问存储在每个列表中每个对象中的数据,您将需要包括另一个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);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM