我有一个元组列表:
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
}
答案 0 :(得分:24)
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
答案 1 :(得分:12)
您正在为错误的对象建立索引。 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);
}
不需要评论解释这些值在这一点上意味着什么,价值观本身会告诉你它们的含义。
答案 2 :(得分: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);
}
答案 3 :(得分: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
}
你去!!!
答案 4 :(得分: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
}
答案 5 :(得分: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
}
答案 6 :(得分: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());
}
}