从包含元组的列表中获取特定项 c#

36

我有一个元组列表:

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个详细信息。假设有一个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       
        }

people[i].Item1,people[i].Item2,people[i].Item3 - Darren Kopp
7个回答

35

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

2
最快的答案赢得奖品。 - Wayneio

14

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

现在不需要解释值的含义,因为这些值本身就能告诉你它们的含义。


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

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       
}

请在这里查看!


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       
}

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

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接