【问题标题】:How to Search a String in List<Tuple<string,string>> in C#如何在 C# 中的 List<Tuple<string,string>> 中搜索字符串
【发布时间】:2012-12-17 12:48:37
【问题描述】:

我有一个

        List<Tuple<string,string>> tr = new List<Tuple<string,string>>();
        tr.Add(new Tuple<string, string>("Test","Add");
        tr.Add(new Tuple<string, string>("Welcome","Update");

        foreach (var lst in tr)
         {
             if(lst.Contains("Test"))
              MessageBox.Show("Value Avail");

          }

我这样做失败了,....

【问题讨论】:

  • “失败”是什么意思?
  • 到底是什么问题?
  • 您在此处发布的代码有很多编译错误。缺少 ')' 和 lst 显然不包含 Contains 的定义。您应该发布可以编译的代码,然后显示您的问题。下面康斯坦丁的回答解决了你的问题。

标签: c# list tuples


【解决方案1】:
List<Tuple<string,string>> tr = new List<Tuple<string,string>>();
tr.Add(new Tuple<string, string>("Test","Add");
tr.Add(new Tuple<string, string>("Welcome","Update");
var index = tr.FindIndex(s=>s.Item1 == "Test" || s.Item2 == "Test");
if(index != -1)
MessageBox.Show("Value Avail");

使用 FindIndex 可以同时检查元素的可用性和索引。

【讨论】:

    【解决方案2】:

    也许这可能对其他人有所帮助。这是我采用的方法:

    List<Tuple<string,string>> tr = new List<Tuple<string,string>>();
    tr.Add(new Tuple<string, string>("Test","Add");
    tr.Add(new Tuple<string, string>("Welcome","Update");
    
    if(lst.Any(c => c.Item1.Contains("Test")))
        MessageBox.Show("Value Avail");
    

    (感谢 here)

    【讨论】:

      【解决方案3】:

      这应该可行:

      foreach (var lst in tr)
      {        
          if (lst.Item1.Equals("Test"))        
              MessageBox.Show("Value Avail");
      }
      

      或者这个

      if (lst.Item1.Equals("Test") || lst.Item2.Equals("Test"))
      

      阅读Tuple Class;您需要通过Item1 和/或Item2 属性访问元组的值。


      为什么要使用元组呢?也许这更容易:

      Dictionary<string, string> dict = new Dictionary<string, string>
      {
          {"Test", "Add"},
          {"Welcome", "Update"}
      };
      
      if (dict.ContainsKey("Test"))
      {
          MessageBox.Show("Value Avail:\t"+dict["Test"]);
      }
      

      【讨论】:

        【解决方案4】:

        如果您想使用 LINQ:

        if(tr.Any(t => t.Item1 == "Test" || t.Item2 == "Test"))
            MessageBox.Show("Value Avail");
        

        如果多次找到文本(如果需要的话),这也将具有仅显示一次消息框的好处。

        【讨论】:

          【解决方案5】:

          改变

          if(lst.Contains("Test"))
          

          到

           if(lst.Item1.Contains("Test") ||  lst.Item2.Contains("Test"))
          

          如果 tuple 有更多项,则需要为所有项添加条件。

          如果你想让它对所有元组通用,你需要使用反射(和the quirky way)。

          【讨论】:

            【解决方案6】:

            为什么要迭代 lstEvntType 而不是 tr?你应该试试这个:

            List<Tuple<string,string>> tr = new List<Tuple<string,string>>();
            tr.Add(new Tuple<string, string>("Test","Add"));
            tr.Add(new Tuple<string, string>("Welcome","Update"));
            List<Tuple<string,string>>  lstEvntType = new List<Tuple<string,string>>();
            
                foreach (var lst in tr)
                {
                    if(lst.Item1.Equals("Test"))
                        MessageBox.Show("Value Avail");
                }
            

            【讨论】:

              【解决方案7】:

              它应该是 foreach (var lst in tr) 而不是 lstEvntType,您应该测试元组的 Item1 字段。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-03-05
                • 1970-01-01
                • 2022-07-31
                • 1970-01-01
                相关资源
                最近更新 更多