2013-07-23 30 views
16

Mam listę krotek:uzyskać konkretną pozycję z listy krotek C#

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

Korzystanie z dataReader mogę zapełnić tę listę z różnymi wartościami:

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

Ale jak mogę przechodzić, zdobywając każdą indywidualną wartość. Na przykład mogę chcieć przeczytać 3 szczegóły dotyczące konkretnej osoby. Powiedzmy, że jest ID, imię i numer telefonu. Chcę coś jak następuje:

 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

ludzie [i] .Item1, ludzie [i] .Item2, ludzie [i] .Item3 –

Odpowiedz

15

people jest lista, więc wskaźnik na listę pierwszy, a następnie można odwołać cokolwiek poz chcesz.

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

Wystarczy pamiętać o rodzajów, że jesteś z pracy, a te rodzaje błędów będzie bardzo nieliczne.

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

Najszybszy odpowiedź wygrywa nagrodę – Wayneio

1

Spróbuj tego:

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

Trzeba przesunąć podziałowe z powrotem trochę:

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

Czy to wszystko, czego szukasz?

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

lub używając foreach:

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

Ty indeksowania niewłaściwy obiekt. people to tablica, którą chcesz indeksować, a nie Item1. Item1 to po prostu wartość dla dowolnego obiektu w kolekcji people. Więc chcesz zrobić coś takiego:

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  
} 

Tak na marginesie, bardzo polecam utworzyć rzeczywisty obiekt, aby utrzymać te wartości zamiast Tuple. Dzięki temu reszta kodu (takiego jak ta pętla) jest znacznie bardziej przejrzysta i łatwa w obsłudze. Może to być coś tak prostego jak:

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

Następnie pętla jest znacznie uproszczona:

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

Nie potrzeba uwag wyjaśniających jakie wartości myśli w tym momencie, to wartości same w sobie powiedzieć, co one oznaczają .

+0

Dobra rada, dzięki – Wayneio

2

Musisz zmienić gdzie indekser jest, trzeba umieścić go w ten sposób:

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  
} 

tam udać !!

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

}