[英]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.