简体   繁体   English

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

[英]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: 使用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. 例如,我可能想要阅读特定人员的3个细节。 Lets say there is an ID, a name and a phone number. 假设有一个ID,一个名字和一个电话号码。 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       
        }

people is 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 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

You're indexing the wrong object. 您正在索引错误的对象。 people is the array that you want to index, not Item1 . people是要索引的数组,而不是Item1 Item1 is simply a value on any given object in the people collection. Item1只是people集合中任何给定对象的值。 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 . Tuple一句,我强烈建议您创建一个实际对象来保存这些值而不是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. 无需评论解释此时值的含义,值本身会告诉您它们的含义。

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

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

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       
    }

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 C# 的元组列表中获取最接近“a”的 4 个元素? - How to get 4 closest to “a” elements from the list of tuples in C#? c#-如何从模型中的项目列表中获取特定项目,并在单击按钮时显示所选项目的属性? - c# - How to get a specific item from a list of items in model and display the properties of the selected item on button click? 如何从开始到另一个列表的特定项目获取项目列表C# - How to get List of items from Begin to Specific item of another list C# C#-如何从通用列表中获取具有最高特定参数的项目? - C# - How to get item from Generic List with the highest specific parameter? c#linq如果项目在列表中并从列表中获取匹配的项目 - c# linq if item is in list and get matching items from list 如何在不循环浏览列表的情况下,获得一个与C#中的其他属性在特定属性上不同的列表项? - How to get one list item which is different on a specific property from the rest in C#, without looping through the list? C#如何从嵌套数组中获取特定项 - C# How to get specific item from nested array 从 checkboxlist c# 中获取未选中的列表项值 - Get unchecked list item value from checkboxlist c# 如何从C#中的n个实现列表中获取项目 - How to get an item from n implemented list in c# 从C#中的数据绑定列表中获取选定的项目详细信息 - Get selected Item details from a data binded List in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM