【问题标题】:Get specific item from list of tuples c#从元组列表中获取特定项目c#
【发布时间】:2013-07-22 19:09:33
【问题描述】:

我有一个元组列表:

List<Tuple<int, string, int>> people = new List<Tuple<int, string, int>>();

使用dataReader,我可以用各种值填充这个列表:

people.Add(new Tuple<int, string, int>(myReader.GetInt32(4), myReader.GetString(3), myReader.GetInt32(5)));

但是我该如何循环,获取每个单独的值。例如,我可能想阅读特定人的 3 个详细信息。假设有一个ID,一个名字和一个电话号码。我想要以下内容:

        for (int i = 0; i < people.Count; i++)
        {
            Console.WriteLine(people.Item1[i]); //the int
            Console.WriteLine(people.Item2[i]); //the string
            Console.WriteLine(people.Item3[i]); //the int       
        }

【问题讨论】:

  • 人[i].Item1,人[i].Item2,人[i].Item3

标签: c# list tuples


【解决方案1】:
class Ctupple
{
    List<Tuple<int, string, DateTime>> tupple_list = new List<Tuple<int, string, DateTime>>();
    public void create_tupple()
    {
        for (int i = 0; i < 20; i++)
        {
            tupple_list.Add(new Tuple<int, string, DateTime>(i, "Dump", DateTime.Now));
        }
        StringBuilder sb = new StringBuilder();
        foreach (var v in tupple_list)
        {
            sb.Append(v.Item1);
            sb.Append("    ");
            sb.Append(v.Item2);
            sb.Append("    ");
            sb.Append(v.Item3);
            sb.Append(Environment.NewLine);
        }
        Console.WriteLine(sb.ToString());
        int j = 0;
    }
    public void update_tupple()
    {
        var vt = tupple_list.Find(s => s.Item1 == 10);
        int index = tupple_list.IndexOf(vt);
        vt = new Tuple<int, string, DateTime>(vt.Item1, "New Value" , vt.Item3);
        tupple_list.RemoveAt(index);
        tupple_list.Insert(index,vt);
        StringBuilder sb = new StringBuilder();
        foreach (var v in tupple_list)
        {
            sb.Append(v.Item1);
            sb.Append("    ");
            sb.Append(v.Item2);
            sb.Append("    ");
            sb.Append(v.Item3);
            sb.Append(Environment.NewLine);
        }
        Console.WriteLine(sb.ToString());
    }

}

【讨论】:

    【解决方案2】:

    你必须改变你的索引器的位置,你必须这样写:

    for (int i = 0; i < people.Count; i++)
    {
        Console.WriteLine(people[i].Item1); //the int
        Console.WriteLine(people[i].Item2); //the string
        Console.WriteLine(people[i].Item3); //the int       
    }
    

    给你!!

    【讨论】:

      【解决方案3】:

      您正在索引错误的对象。 people 是您要索引的数组,而不是 Item1Item1 只是 people 集合中任何给定对象的一个​​值。所以你会做这样的事情:

      for (int i = 0; i < people.Count; i++)
      {
          Console.WriteLine(people[i].Item1); //the int
          Console.WriteLine(people[i].Item2); //the string
          Console.WriteLine(people[i].Item3); //the int       
      }
      

      顺便说一句,我强烈建议您创建一个实际对象来保存这些值,而不是 Tuple。它使其余的代码(例如这个循环)更加清晰和易于使用。可能很简单:

      class Person
      {
          public int ID { get; set; }
          public string Name { get; set; }
          public int SomeOtherValue { get; set; }
      }
      

      那么循环就大大简化了:

      foreach (var person in people)
      {
          Console.WriteLine(person.ID);
          Console.WriteLine(person.Name);
          Console.WriteLine(person.SomeOtherValue);
      }
      

      此时不需要 cmets 解释值的含义,值本身会告诉您它们的含义。

      【讨论】:

        【解决方案4】:

        这就是你要找的全部吗?

        for (int i = 0; i < people.Count; i++)
        {
            Console.WriteLine(people[i].Item1);
            Console.WriteLine(people[i].Item2);
            Console.WriteLine(people[i].Item3);       
        }
        

        或使用foreach

        foreach (var item in people) 
        {
            Console.WriteLine(item.Item1);
            Console.WriteLine(item.Item2);
            Console.WriteLine(item.Item3);
        }
        

        【讨论】:

          【解决方案5】:

          您需要将索引器向后移动一点:

          for (int i = 0; i < people.Count; i++)
          {
              Console.WriteLine(people[i].Item1); //the int
              Console.WriteLine(people[i].Item2); //the string
              Console.WriteLine(people[i].Item3); //the int       
          }
          

          【讨论】:

            【解决方案6】:

            试试这个:

            for (int i = 0; i < people.Count; i++)
                {
                    Console.WriteLine(people[i].Item1); //the int
                    Console.WriteLine(people[i].Item2); //the string
                    Console.WriteLine(people[i].Item3); //the int       
                }
            

            【讨论】:

              【解决方案7】:

              people 是一个列表,因此您首先对列表进行索引,然后您可以引用任何您想要的项目。

              for (int i = 0; i < people.Count; i++)
              {
                  people[i].Item1;
                  // Etc.
              }
              

              请记住您正在使用的类型,这样的错误将很少见。

              people;          // Type: List<T> where T is Tuple<int, string, int>
              people[i];       // Type: Tuple<int, string, int>
              people[i].Item1; // Type: int
              

              【讨论】:

              • 最快回答得奖
              猜你喜欢
              • 2012-12-30
              • 1970-01-01
              • 2013-10-26
              • 2011-11-22
              • 2022-12-12
              • 1970-01-01
              • 1970-01-01
              • 2019-08-12
              • 1970-01-01
              相关资源
              最近更新 更多