替换对象列表中的对象

时间:2014-11-05 08:49:24

标签: c# .net list variable-assignment generic-list

在C#中,如果我有一个List<T>,并且我有T类型的对象,我该如何用List<T>类型的对象替换T中的特定项目1}}?

以下是我的尝试:

List<CustomListItem> customListItems = new List<CustomListItem>();
CustomListItem customListItem1 = new CustomListItem() { name = "Item 1", date = DateTime.MinValue};
CustomListItem customListItem2 = new CustomListItem() { name = "Item 2", date = DateTime.MinValue };
CustomListItem customListItem3 = new CustomListItem() { name = "Item 3", date = DateTime.MinValue };

customListItems.Add(customListItem1);
customListItems.Add(customListItem2);
customListItems.Add(customListItem3);

CustomListItem newCustomListItem = new CustomListItem() { name = "Item 4", date = DateTime.Now };

customListItem2 = customListItems.Where(i=> i.name == "Item 2").First();
customListItem2 = newCustomListItem;

在上面的代码中,我想将customListItem2替换为newCustomListItem

我是否必须删除列表中的项目,然后插入新项目?我可以不对customListItem2 = newCustomListItem进行简单的分配吗?

用另一个项目替换列表中项目的最有效方法是什么?

提前致谢

5 个答案:

答案 0 :(得分:38)

您必须替换项目,而不是customListItem2的值。只需替换以下内容:

customListItem2 = customListItems.Where(i=> i.name == "Item 2").First();
customListItem2 = newCustomListItem;

有了这个:

customListItem2 = customListItems.Where(i=> i.name == "Item 2").First();
var index = customListItems.IndexOf(customListItem2);

if(index != -1)
    customListItems[index] = newCustomListItem;

修改

在评论中说明Roman R.,您可以用简单的.Where(predicate).First()替换First(predicate)

customListItem2 = customListItems.First(i=> i.name == "Item 2");

答案 1 :(得分:8)

var customListItems = new List<CustomListItem>();
var customListItem1 = new CustomListItem() { name = "Item 1", date = DateTime.MinValue };
var customListItem2 = new CustomListItem() { name = "Item 2", date = DateTime.MinValue };
var customListItem3 = new CustomListItem() { name = "Item 3", date = DateTime.MinValue };

customListItems.Add(customListItem1);
customListItems.Add(customListItem2);
customListItems.Add(customListItem3);

var newCustomListItem = new CustomListItem() { name = "Item 4", date = DateTime.Now };

customListItems[customListItems.FindIndex(x => x.name == "Item 2")] = newCustomListItem;

public static class ListExtensions
{
    public static void Replace<T>(this List<T> list, Predicate<T> oldItemSelector , T newItem)
    {
        //check for different situations here and throw exception
        //if list contains multiple items that match the predicate
        //or check for nullability of list and etc ...
        var oldItemIndex = list.FindIndex(oldItemSelector);
        list[oldItemIndex] = newItem;
    }
}

然后

customListItems.Replace(x => x.name == "Item 2", newCustomListItem);

答案 2 :(得分:2)

如果列表顺序对你不重要的话 你可以试试这个

CustomListItem newCustomListItem = new CustomListItem() { name = "Item 4", date = DateTime.Now };

customListItem2 = customListItems.Where(i=> i.name == "Item 2").First();  

customListItems.Remove(customListItem2);
customListItems.Add(newCustomListItem );

答案 3 :(得分:0)

您可以使用select来创建带有替换项的新iEnumerable:

customListItems.Select(x => x.name == "Item 2" ? newItem : x).ToList();

我没有测试性能,但是我发现此解决方案非常简洁明了。

答案 4 :(得分:0)

使用

DynamicData.LisEx.Replace<T>(T source, T originalItem, T newItem)