.net 替换对象列表中的对象

jexiocij  于 2022-12-20  发布在  .NET
关注(0)|答案(5)|浏览(198)

在C#中,如果我有一个List<T>,并且我有一个T类型的对象,我如何用T类型的对象替换List<T>中的特定项?
下面是我的尝试:

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;

在上面的代码中,我想用newCustomListItem替换customListItem2
我是否必须删除列表中的项,然后插入新项?我不能对customListItem2 = newCustomListItem进行简单的赋值吗?
用列表中的一个项目替换另一个项目的最有效方法是什么?
先谢了

nwlls2ji

nwlls2ji1#

您必须替换项目,而不是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.在评论中所说的,可以用简单的First(predicate)替换.Where(predicate).First()

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

czq61nw12#

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);
2exbekwf

2exbekwf3#

如果列表顺序对您不重要,那么您可以尝试以下操作

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 );
rjee0c15

rjee0c154#

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

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

我没有测试性能,但我发现这个解决方案非常简洁和明确。

vsmadaxz

vsmadaxz5#

另一种方法是使用DynamicData包,它的列表中有很多其他工具:

快速解答

确保软件包已安装:

Install-Package DynamicData -Version 7.4.3

那么

using DynamicData;

DynamicData.ListEx.Replace<CustomListItem>(customListItems, customListItem2, newCustomListItem);

以下是完整的工作溶液。

工作示例

我的自定义项目类

public class CustomListItem
{
    public string name; 
    public DateTime date;

    public override string ToString()
    {
        return name + ", " + date.ToString();
    }
}

大体上试试这个,确保在替换之前注解掉赋值。

static void Main(string[] args)
{
    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;

    DynamicData.ListEx.Replace<CustomListItem>(customListItems, customListItem2, newCustomListItem);

    foreach(var customListItem in customListItems)
    {
        Debug.Print("\n" + customListItem.ToString());
    }
}

references: https://github.com/reactivemarbles/DynamicData.
https://dynamic-data.org

相关问题