2013-07-23 14 views
16

मैं tuples की एक सूची है:tuples की सूची में से विशिष्ट आइटम प्राप्त 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 विवरण पढ़ना चाहता हूं। आइए कहें कि एक आईडी, एक नाम और एक फोन नंबर है। मैं निम्नलिखित की तरह कुछ हैं:

 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 वह सरणी है जिसे आप इंडेक्स करना चाहते हैं, Item1 नहीं। Item1people संग्रह में किसी भी दिए गए ऑब्जेक्ट पर बस एक मान है। तो अगर आप कुछ इस तरह करते हैं:

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

} 
संबंधित मुद्दे