-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