迭代IDictionary/Dictionary对象

本文关键字:对象 Dictionary IDictionary 迭代 | 更新日期: 2023-09-27 18:00:50

我有一个代码,其中声明了这一点:

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

迭代字典对象中所有varNames的最简单方法是什么?像前臂吗?

迭代IDictionary/Dictionary对象

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

这将遍历字典中的所有KeyValuePair<T, T>

只需为已经发布的精彩答案添加一个替代方案,您可以执行以下操作:

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