2013-07-23 186 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()); 
    } 

}