迭代IDictionary / Dictionary对象

时间:2011-06-28 23:19:20

标签: c# asp.net dictionary loops

我有代码声明:

 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的最简单方法是什么?有没有像foreach一样?

3 个答案:

答案 0 :(得分:3)

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

这会遍历词典中的所有KeyValuePair<T, T>

答案 1 :(得分:3)

只需添加已发布的优秀答案的替代方案,您就可以:

dict_Options.Keys.ToList().ForEach(m => SomeFunc(m.Value.varName));

答案 2 :(得分:2)

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