gpt4 book ai didi

c# - 迭代C#中的集合

转载 作者:太空宇宙 更新时间:2023-11-03 17:58:12 25 4
gpt4 key购买 nike

我创建了以下类:

[Serializable]
public class AppContext : IXmlSerializable {
public string reportType = string.Empty;
public string entityId = string.Empty;

// OPTIONS
public IDictionary<string, OPTIONS> dict_Options = new Dictionary<string, OPTIONS>();
// Advanced Options
public List<string> listAdvancedOptions = new List<string>();



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


我如何遍历选项以获取所有varNumbers?

最佳答案

方法一

foreach (KeyValuePair<string, AppContext.OPTIONS> kvp in appContext.dict_Options)
{
Console.WriteLine(kvp.Value.varNumber);
}


方法二

foreach (AppContext.OPTIONS item in appContext.dict_Options.Values)
{
Console.WriteLine(item.varNumber);
}


方法3

foreach (string item in appContext.dict_Options.Select(x => x.Value.varNumber))
{
Console.WriteLine(item);
}

关于c# - 迭代C#中的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5916167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com