-2
我有對象的名單,我想替換的對象之一與新對象名單:如何更新列表C#中的整個對象?
public Parent AppendParentChildren(Request request)
{
var Children = request.Parent.Children.ToList();
if (Children.Any(x => x.TrackingNumber == request.Child.TrackingNumber))
{
//Here I want to replace any Children that have the same tracking number in the list with the new Child passed in
}
else
{
Children.Add(request.Child);
}
request.Parent.Children = Children;
return request.Parent;
}
public class Request
{
public Parent Parent { get; set; }
public Child Child { get; set; }
}
public class Parent
{
public IEnumerable<Child> Children {get;set;}
}
如果我嘗試在一個循環中使用它:
public static class Extension
{
public static void Update<T>(this List<T> items, T newItem)
{
foreach (var item in items)
{
//this
item = newItem;
}
}
}
項目是隻讀的,所以我不能替換列表中的對象。
有什麼建議嗎?
您是否聽說過_loops_? – 2015-02-09 16:45:37
我不明白你在做什麼,對不起。你能編輯你的文章並澄清嗎? – 2015-02-09 16:48:07
您確定要用相同的對象替換具有相同跟蹤編號的多個對象嗎?您將在該列表中將對象複製到您的列表中。 – 2015-02-09 16:50:42