2013-07-23 138 views
16

我有元组的列表:获取特定项目C#

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  
     } 
+0

人[I] .Item1,人[I] .Item2,人[I] .Item3 –

回答

15

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 
+1

最快的答案赢取奖品 – Wayneio

1

试试这个:

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  
    } 
1

你需要移动索引回位:

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

难道这一切你要找的?

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); 
} 
9

你索引错了对象。 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); 
} 

无需注释解释什么值的含义在这一点上,这些值本身告诉你他们是什么意思。

+0

好的建议,谢谢 – Wayneio

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  
} 

你去那里!

0
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()); 
    } 

}