简体   繁体   English

迭代一个字典/字典 Object

[英]Iterate a IDictionary/Dictionary Object

I have code where this is declared:我有声明它的代码:

 public IDictionary<string, OPTIONS> dict_Options = new Dictionary<string, OPTIONS>();

 public class OPTIONS
 {
        public string subjectId = string.Empty;
        public string varNumber = string.Empty;
        public string varName = string.Empty;
 }

What's the easiest way to iterate over all the varNames in my dictionary object?遍历我的字典 object 中所有 varNames 的最简单方法是什么? Is there like a foreach?有没有像foreach这样的?

        foreach (var item in dict_Options)
        {
            string varName = item.Value.varName;
        }

This iterates through all the KeyValuePair<T, T> in your dictionary这将遍历字典中的所有KeyValuePair<T, T>

Just to add an alternative to the great answers already posted, you could do:只需为已发布的出色答案添加替代方案,您可以执行以下操作:

dict_Options.Keys.ToList().ForEach(m => SomeFunc(m.Value.varName));
foreach(var name in dict_Options.Select(x => x.Value.varName))
{
    SomeFunc(name);
}

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

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