【问题标题】:Edit a List<Tuple> item编辑 List<Tuple> 项
【发布时间】:2014-11-29 02:32:24
【问题描述】:

使用List&lt;String&gt;,您可以简单地编辑项目:

var index = List.FindIndex(s => s.Number == box.Text);
List[index] = new String;

但是,例如,如何将其应用于List&lt;Tuple&lt;string, string&gt;&gt;

【问题讨论】:

  • @MattBurland 对于s 没有扩展名NumberList&lt;Tuple&gt; .. 但显然使用AD.Net 的解决方案我必须使用s.item1

标签: c# list tuples


【解决方案1】:

您可以通过类似的方式找到索引 - 通过将条件应用于列表中的每个元组:

var index = listOfTuples.FindIndex(t => t.Item1 == box1.Text || t.Item2 == box2.Text);

您可以通过调用Tuple.Create来替换项目:

listOfTuples[index] = Tuple.Create(box3.Text, box4.Text);

【讨论】:

  • 这对我来说很好用,Tuple.Create 正是我所需要的
【解决方案2】:
var tuple = List.Find(s => s.Item1 == box.Text); 
//assuming you're searching for the first string, but you can change the predicate anyway. 
tuple = new Tuple<string, string>(new String, tuple.Item2);

正如另一个答案中提到的,您当然也可以使用索引,但您也可以只找到对象并更新它,这应该也可以。

【讨论】:

  • 这并没有改变我的列表。它只是创建了一个被丢弃的局部变量。
  • 下面的答案对我有用,这个刚刚创建了一个本地变量
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 2023-03-10
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
相关资源
最近更新 更多