Wayneio
Wayneio

Reputation: 3576

Get specific item from list of tuples c#

I have a list of tuples:

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

Using a dataReader, I may populate this list with various values:

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

But then how do I loop through, getting each individual value. For example I may want to read the 3 details for a specific person. Lets say there is an ID, a name and a phone number. I want something like the following:

        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       
        }

Upvotes: 36

Views: 63974

Answers (7)

Saurabh
Saurabh

Reputation: 1631

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

}

Upvotes: 0

Dozer789
Dozer789

Reputation: 2036

You got to change where your indexer is, you have to put it like this:

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       
}

There you go!!

Upvotes: 2

David
David

Reputation: 219087

You're indexing the wrong object. people is the array that you want to index, not Item1. Item1 is simply a value on any given object in the people collection. So you'd do something like this:

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       
}

As an aside, I highly recommend you create an actual object to hold these values instead of a Tuple. It makes the rest of the code (such as this loop) much more clear and easy to work with. It could be something as simple as:

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

Then the loop is greatly simplified:

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

No need for comments explaining what the values mean at this point, the values themselves tell you what they mean.

Upvotes: 14

Tim
Tim

Reputation: 15247

Is this all you're looking for?

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

or using a foreach:

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

Upvotes: 3

It&#39;sNotALie.
It&#39;sNotALie.

Reputation: 22814

You need to move the indexer back a bit:

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       
}

Upvotes: 1

Daniel M&#246;ller
Daniel M&#246;ller

Reputation: 86650

Try this:

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       
    }

Upvotes: 1

voithos
voithos

Reputation: 70632

people is a list, so you index into the list first, and then you can reference whatever item you want.

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

Just keep in mind the types that you're working with, and these kinds of mistakes will be few and far between.

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

Upvotes: 36

Related Questions