gpt4 book ai didi

c# - 比较两个对象列表的新的、更改的、更新的特定属性

转载 作者:可可西里 更新时间:2023-11-01 02:59:06 24 4
gpt4 key购买 nike

我一直在尝试并失败了一段时间,以找到一种解决方案来根据对象的属性与对象列表进行比较。我读过其他类似的解决方案,但它们要么不合适(要么我不明白答案!)。

代码是C#

我有一个表示图像的模型

public class AccommodationImageModel
{
public int Id { get; set; }
public string Path { get; set; }
public string Caption { get; set; }
public string Description { get; set; }
public bool CoverImage { get; set; }
public bool Visible { get; set; }
}

我有这个模型的两个列表。一个是现有列表,另一个是更新列表。我需要比较这两个列表,看看哪些已被删除、更新或新增。

我不需要比较整个对象,只比较它们的属性 Id。

List<AccommodationImageModel> masterList;
List<AccommodationImageModel> compareList;

如果 compareList 包含任何 Id=0 的 AccommodationImageModel,那么它们是新的,因为新条目尚未分配 Id。

待删除

如果 masterList 包含任何 AccommodationImageModel,其 ID 不在 compareList 中,那么它们将被删除,因为它们已从 compareList 中删除,应该从 masterList 中删除。因此,我需要一份需要删除的 list 。

待更新

如果 newList 和 masterList 的 Id 相同,那么它们将被更新。因此我需要一个共享相同 ID 的列表,这样我就可以更新它们。我不太担心这些模型是否相同并且不需要更新,因为每个列表只有一小部分,所以即使它们没有更改,是否更新也没有太大关系。

这三个结果中的每一个都需要作为 AccommodationImageModel 列表返回,以便我可以执行适当的更新、删除、添加。

编辑

我在下面使用我从 ATM 中选择的解决方案添加了 3 种测试方法,展示了它的工作实现。

测试方法

[TestMethod]
public void Test_Deleted_Image()
{
// set up the masterList
List<AccommodationImageModel> masterList = new List<AccommodationImageModel>();
masterList.Add(new AccommodationImageModel { Id = 1 });
masterList.Add(new AccommodationImageModel { Id = 2 });

// set up the compare list
List<AccommodationImageModel> compareList = new List<AccommodationImageModel>();
compareList.Add(new AccommodationImageModel { Id = 1 });
compareList.Add(new AccommodationImageModel { Id = 3 });
compareList.Add(new AccommodationImageModel { Id = 0 });

// get the deleted models
List<AccommodationImageModel> result = masterList.Where(c => !compareList.Any(d => d.Id == c.Id)).ToList();

// result should hold first model with id 2
Assert.AreEqual(2, result.FirstOrDefault().Id);
}

[TestMethod]
public void Test_Added_Image()
{
// set up the masterList
List<AccommodationImageModel> masterList = new List<AccommodationImageModel>();
masterList.Add(new AccommodationImageModel { Id = 1 });
masterList.Add(new AccommodationImageModel { Id = 2 });

// set up the compare list
List<AccommodationImageModel> compareList = new List<AccommodationImageModel>();
compareList.Add(new AccommodationImageModel { Id = 1 });
compareList.Add(new AccommodationImageModel { Id = 3 });
compareList.Add(new AccommodationImageModel { Id = 0 });

// get the added models
List<AccommodationImageModel> result = compareList.Where(c => c.Id == 0).ToList();

// result should hold first model with id 0
Assert.AreEqual(0, result.FirstOrDefault().Id);
}

[TestMethod]
public void Test_Updated_Image()
{
// set up the masterList
List<AccommodationImageModel> masterList = new List<AccommodationImageModel>();
masterList.Add(new AccommodationImageModel { Id = 1 });
masterList.Add(new AccommodationImageModel { Id = 2 });

// set up the compare list
List<AccommodationImageModel> compareList = new List<AccommodationImageModel>();
compareList.Add(new AccommodationImageModel { Id = 1 });
compareList.Add(new AccommodationImageModel { Id = 3 });
compareList.Add(new AccommodationImageModel { Id = 0 });

// get the updated models
List<AccommodationImageModel> result = masterList.Where(c => compareList.Any(d => c.Id == d.Id)).ToList();

// result should hold first model with id 1
Assert.AreEqual(1, result.FirstOrDefault().Id);
}

最佳答案

简单的 Linq

List<AccommodationImageModel> toBeAdded = compareList.Where(c=>c.Id==0).ToList();

待删除

List<AccomodationImageModel> toBeDeleted = masterList.Where(c => !compareList.Any(d => c.Id == d.Id)).ToList();

待更新

List<AccomodationImageModel> toBeUpdated = masterList.Where(c => compareList.Any(d => c.Id == d.Id)).ToList();

关于c# - 比较两个对象列表的新的、更改的、更新的特定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23581379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com