从元组c#列表中获取特定项
本文关键字:获取 元组 列表 | 更新日期: 2023-09-27 18:24: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
是一个列表,因此您可以先索引到列表中,然后可以引用您想要的任何项目。
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
您正在索引错误的对象。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);
}
在这一点上,不需要注释来解释价值观的含义,价值观本身会告诉你它们的含义。
这就是你想要的吗?
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);
}
你必须改变索引器的位置,你必须这样放:
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
}
给你!!
试试这个:
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
}
您需要将索引器向后移动一点:
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());
}
}