Compare two lists of object for new, changed, updated on a specific property

前端 未结 4 2110
时光取名叫无心
时光取名叫无心 2020-12-15 06:06

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 w

4条回答
  •  清歌不尽
    2020-12-15 06:26

    Simple Linq

    New

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

    To be deleted

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

    To be updated

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

提交回复
热议问题