33

我有一个元组列表:

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

使用 a 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       
        }
4

7 回答 7

33

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
于 2013-07-23T15:45:23.273 回答
14

您正在索引错误的对象。 people是您要索引的数组,而不是Item1. Item1只是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);
}

无需评论来解释此时值的含义,值本身会告诉您它们的含义。

于 2013-07-23T15:47:50.440 回答
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);
}
于 2013-07-23T15:46:13.153 回答
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       
}

给你!!

于 2013-07-23T15:49:28.620 回答
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       
    }
于 2013-07-23T15:45:35.653 回答
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       
}
于 2013-07-23T15:45:52.370 回答
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());
    }

}
于 2017-03-25T10:27:23.410 回答