【问题标题】:accessing a Dictionary<Tuple, int>访问 Dictionary<Tuple, int>
【发布时间】:2020-06-18 05:32:41
【问题描述】:

声明

 private Dictionary<Tuple<String, int>, int> securityLevel;

在构造函数内部进行初始化

        securityLevel.Add(Tuple.Create("Agent", 0), 1);
        securityLevel.Add(Tuple.Create("DoubleAgent", 500), 2);
        securityLevel.Add(Tuple.Create("FieldOfficeManager", 2105086), 3);
        securityLevel.Add(Tuple.Create("DivisionChief", 8388350), 4);
        securityLevel.Add(Tuple.Create("RegionalManager", 2104960), 5);
        securityLevel.Add(Tuple.Create("ChiefInformationSecurityOfficer", 6307582), 6);

我的方法

 private int getLowestSecurityLevel(Dictionary<Tuple, int> entry)
    {
        int currentSecurity = 0;
        // not sure how to iterate a Dictionary with a Tuple? I want to get the title of the Geek Squad agent
       foreach (KeyValuePair<Tuple<String, int>, int> item in entry)
       {
       }
    }

Visual Studio 告诉我

无法转换 'System.Collections.Generic.KeyValuePair, int>' 到 'System.Collections.Generic.KeyValuePair'

【问题讨论】:

  • “我想获得 Geek Squad 特工的头衔”- 来自什么?从 currentSecurity = 0 匹配元组中的 int,即您想要“代理”?如果这是您需要匹配的值,为什么不是 int 字典的键而不是整个元组?字典值中的 1-6 代表什么?
  • @Rup 我更新了问题以包括我如何声明它并提供有关它如何初始化的信息
  • 好的,但这不是我问的。
  • currentSecurity 值如何属于Tuple&lt;String, int&gt;, int?
  • 那么逻辑是如果传递的字典不包含任何条目,那么我们返回 0,否则我们将“currentSecurity”设置为最低的 int。但是它的使用方式并不能帮助我迭代字典

标签: c# dictionary tuples


【解决方案1】:

见Tuple Deconstruction

private void GetLowestentry(Dictionary<Tuple<string, int>, int> entry)
            {
                int currentSecurity = 0;

                foreach(KeyValuePair<Tuple<string, int>, int> keyValue in entry)
                {
                    // Your condition
                    if(keyValue.Key.Item1 == "Your Name")
                    {
                        // Select Anything you want
                        currentSecurity = keyValue.Key.Item2;
                        //currentSecurity = keyValue.Value;
                    }
                }

         }

【讨论】:

    【解决方案2】:

    将您的字典声明为:

    Dictionary<(string, int), int>
    

    这将使用 C# 中的新元组支持。现在应该避免使用旧的 Tuple 类,并且该语言已内置支持新的 ValueTuple 结构。

    像这样添加值:

    securityLevel.Add(("Agent", 0), 1);
    securityLevel.Add(("DoubleAgent", 500), 2);
    securityLevel.Add(("FieldOfficeManager", 2105086), 3);
    securityLevel.Add(("DivisionChief", 8388350), 4);
    securityLevel.Add(("RegionalManager", 2104960), 5);
    securityLevel.Add(("ChiefInformationSecurityOfficer", 6307582), 6);
    

    并将您的方法更改为:

    private int getLowestSecurityLevel(Dictionary<(string, int), int> entry)
    

    【讨论】:

    • 为什么?我需要改变我构建项目的方式吗?
    • 这将如何帮助 OP 获得想要的(大部分未指定)答案?
    【解决方案3】:

    如果你的目标是匹配int currentSecurity = 0; 反对

    securityLevel.Add(Tuple.Create("Agent", 0), 1);
    

    那么你想要

    string title = entry.Keys.FirstOrDefault(e => e.Item2 == currentSecurity)?.Item1;
    

    (或 SingleOrDefault)。但是,这没有正确使用字典。如果您需要按此数字(元组中的第二个)查找字符串,则应将其设为字典键,即

    var securityLevel = new Dictionary<int, string>();
    securityLevel.Add(0, "Agent");
    

    假设您不需要额外的 int 1 ,您当前保存为该元组的字典中的值。

    【讨论】:

      猜你喜欢
      • 2018-01-25
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      相关资源
      最近更新 更多