所以我有一个列表,在我的方法中我试图返回一个带有修改的新列表。
但问题是我对线索列表的 ID 所做的更改也正在对我传递的线索列表进行。
public List<Clue> NewOrderList(List<Clue> clues, int[] ids)
{
var newClueOrder = new List<Clue>();
// For each ID in the given order
for (var i = 0; i < ids.Length; i++)
{
// Get the original clue that matches the given ID
var clue = clues.First(clue1 => clue1.Id == ids[i]);
// Add the clue to the new list.
newClueOrder.Add(clue);
// Retain the ID of the clue
newClueOrder[i].Id = clues[newClueOrder.Count - 1].Id;
}
return newClueOrder;
}
为什么会这样,最好的解决方案是什么?我见过类似的问题,但老实说,我不太明白解决方案到底是什么。