2013-07-23 95 views
16

Tôi có một danh sách các hàng:Get mục cụ thể từ danh sách các hàng C#

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

Sử dụng một dataReader, tôi có thể vào danh sách này với các giá trị khác nhau:

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

Nhưng sau đó làm thế nào để tôi lặp lại, nhận từng giá trị riêng lẻ. Ví dụ tôi có thể muốn đọc 3 chi tiết cho một người cụ thể. Giả sử có ID, tên và số điện thoại. Tôi muốn một cái gì đó như sau:

 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

mọi người [i] .Item1, people [i] .Item2, people [i] .Item3 –

Trả lời

15

people là một danh sách, vì vậy bạn chỉ mục vào danh sách đầu tiên, và sau đó bạn có thể tham khảo bất cứ mục mà bạn muốn.

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

Chỉ cần ghi nhớ các loại mà bạn đang làm việc với, và các loại sai lầm sẽ ít và xa giữa.

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

Câu trả lời nhanh nhất giành được giải thưởng – Wayneio

1

Hãy thử điều này:

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

Bạn cần phải di chuyển indexer lại một chút:

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

Đây có phải là tất cả các bạn đang tìm kiếm?

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

hoặc sử dụng một foreach:

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

Bạn đang lập chỉ mục các đối tượng sai. people là mảng mà bạn muốn lập chỉ mục, không phải là Item1. Item1 chỉ đơn giản là một giá trị trên bất kỳ đối tượng cụ thể nào trong bộ sưu tập people. Vì vậy, bạn muốn làm điều gì đó như thế này:

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  
} 

Là một sang một bên, tôi khuyên bạn nên tạo một đối tượng thực tế để giữ các giá trị thay vì một Tuple. Nó làm cho phần còn lại của mã (như vòng lặp này) rõ ràng hơn và dễ làm việc hơn. Nó có thể là một cái gì đó đơn giản như:

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

Sau đó, vòng lặp là rất đơn giản:

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

Không cần phải xin ý kiến ​​giải thích những gì các giá trị có nghĩa là vào thời điểm này, các giá trị bản thân cho bạn biết ý nghĩa của chúng .

+0

Lời khuyên tốt, cảm ơn – Wayneio

2

Bạn đã thay đổi nơi indexer của bạn, bạn cần phải đặt nó như thế này:

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  
} 

Có bạn đi !!

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

}