比较两个对象列表以了解特定属性的新的、更改的、更新的 [英] Compare two lists of object for new, changed, updated on a specific property

查看:25
本文介绍了比较两个对象列表以了解特定属性的新的、更改的、更新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been trying and failing for a while to find a solution to compare to lists of objects based on a property of the objects. I've read other similar solutions but they were either not suitable (or I didn't understand the answer!).

代码是 C#

我有一个代表图像的模型

I have a model that represents an image

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; }     
}

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

I have two lists of this model. One is the existing list, another is an updated list. I need to compare the two lists to see which have been removed, updated or are new.

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

I don't need to compare the whole object, just compare them on their property Id.

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

如果 compareList 包含任何 Id=0 的 AccommodationImageModel,那么它们是新的,因为新条目还没有分配 Id.

New

If compareList contains any AccommodationImageModel with Id=0 then they are new because new entries do not have an Id assigned yet.

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

If masterList contains any AccommodationImageModel with Ids that are Not in compareList then they are to be deleted, because they have been removed from the compareList and should be removed from the masterList. Therefore I need a list of the ones that need to be deleted.

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

If newList and masterList have Id's that are the same then they are to be updated. Therefore I need a list of the ones that share the same ID, so I can them update them. I'm not too concerned if these models are identical and don't need updating as there will only be a small number per list so it doesn't matter much if they get updated even if they haven't changed.

三个结果中的每一个都需要作为一个住宿图像模型列表返回,以便我可以执行适当的更新、删除、添加.

Each of the three results needs to be returned as a List of AccommodationImageModel so that I can then carry out the appropriate update, remove, add.

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

I've added 3 test methods below with my chosen solution from ATM, showing its working implementation.

[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);
    }

推荐答案

Simple Linq

Simple 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();

这篇关于比较两个对象列表以了解特定属性的新的、更改的、更新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆