【问题标题】:How to check for key-value pair mapping in this scenario?在这种情况下如何检查键值对映射?
【发布时间】:2017-07-16 04:16:07
【问题描述】:

我想定义一个结构来帮助我维护这个键值对列表 -

"ABC", "010"
"ABC", "011",
"BAC", "010"
"BAC" , "011"
"CAB", "020"

然后我想写一个方法来传入 ("ABC" , "010" ) 看看这个映射是否存在 & 如果它存在则方法返回 true。

我应该使用什么结构以及方法看起来如何?

我试过了——

 public bool IsAllowed(string source, string dest)
        {
            bool allowed = false;
            var allowedDest = new List<KeyValuePair<string, string>>()
            {
                new KeyValuePair<string, string>("ABC","010"),
                new KeyValuePair<string, string>("ABC","011"),
                new KeyValuePair<string, string>("BAC","010"),                
                new KeyValuePair<string, string>("BAC","011"),
                new KeyValuePair<string, string>("CAB","020"),
                new KeyValuePair<string, string>("CAB","030")
            };

             // How to check for mapping?

            return allowed;
        }

【问题讨论】:

  • 这个怎么样:allowed = allowedDest.Any(c =&gt; c.Equals(new KeyValuePair&lt;string, string&gt;("ABC", "010")));
  • 感觉就像你只想使用Dictionary&lt;string, string&gt;。然后用TryGetValue检查给定键的值是否存在;如果返回 true,则映射存在。

标签: c# .net linq c#-4.0 key-value


【解决方案1】:

如果列表很大,我会声明

HashSet<Tuple<string,string>> Allowed = new HashSet<Tuple<string,string>>();

Allowed.Add(Tuple.Create<string,string>("ABC","010");
[... and all the others]

if (Allowed.Contains(Tuple.Create<string,string>("ABC","010")) { }

如果它是一个小列表,您可以使用 foreach 语句或 .Any() 命令对其进行迭代。

【讨论】:

    【解决方案2】:

    您可以保持简单并使用字符串数组。

    public bool IsAllowed(string source, string dest)
    {
        var allowedDest = new []
        {
            new [] {"ABC", "010"},
            new [] {"ABC", "011"},
            new [] {"BAC", "010"}
            //...
        };
    
        var match = new [] { source, dest };
    
        return allowedDest.Any(x => x.SequenceEqual(match));
    }
    

    【讨论】:

      【解决方案3】:

      你需要在这里使用 Linq 方法。

      您可以使用 FirstOrDefault 方法通过比较列表中项目的键和值来从列表中检索具有匹配源和目标的项目。

      如果找到的项目将被返回,否则默认的 KeyValuePair 将被返回。 然后你需要检查它是否返回默认值,并根据它返回true或false。

      public bool IsAllowed(string source, string dest)
          {
              bool allowed = false;
              var allowedDest = new List<KeyValuePair<string, string>>()
              {
                  new KeyValuePair<string, string>("ABC","010"),
                  new KeyValuePair<string, string>("ABC","011"),
                  new KeyValuePair<string, string>("BAC","010"),                
                  new KeyValuePair<string, string>("BAC","011"),
                  new KeyValuePair<string, string>("CAB","020"),
                  new KeyValuePair<string, string>("CAB","030")
              };
              var item = allowedDest.FirstOrDefault(kvpair => kvpair.Key == source && kvpair.Value == dest);
              allowed = !item.Equals(default(KeyValuePair<string, string>));
      
              return allowed;
          }
      

      这应该可以帮助您解决问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-07-12
        • 1970-01-01
        • 2015-04-07
        • 2012-07-01
        • 2012-01-24
        • 2016-09-21
        • 2022-08-21
        相关资源
        最近更新 更多