从元组列表中获取特定项目 c#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17814856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:32:53  来源:igfitidea点击:

Get specific item from list of tuples c#

c#listtuples

提问by Wayneio

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:

使用 a dataReader,我可以用各种值填充这个列表:

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:

但是,我如何循环遍历,获取每个单独的值。例如,我可能想阅读特定人的 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       
        }

采纳答案by voithos

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

people是一个列表,让你索引列表中第一个,然后你可以引用任何你想要的物品。

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

Just keep in mind the typesthat 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

回答by Daniel M?ller

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       
    }

回答by It'sNotALie.

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       
}

回答by Tim

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

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

回答by David

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

您正在索引错误的对象。 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       
}

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:

顺便说一句,我强烈建议您创建一个实际对象来保存这些值而不是Tuple. 它使其余代码(例如此循环)更加清晰和易于使用。它可能很简单:

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.

此时无需注释解释这些值的含义,值本身会告诉您它们的含义。

回答by Dozer789

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!!

给你!!

回答by Saurabh

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

}