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

前端 未结 4 2102
时光取名叫无心
时光取名叫无心 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:16

    One simple approach would be to override the == operator in yourAccomodationImageModel as such:

    public static override bool operator ==(AccommodationImageModel a, AccommodationImageModel b)
    {
        return a.Id == b.Id;
    }
    

    Then when comparing just check the master list against the comparer list and remove those in the master list that don't have an identical object in the comparer list:

    List<AccomodationImageModel> rem = new List<AccomodationImageModel>;
    List<AccomodationImageModel> newobj = new List<AccomodationImageModel>;
    foreach(AccomodationImageModel a in compareList) {
              if(a.Id == 0) { // id == 0 => new item
                     newobj.Add(a); // add new item later
              } else {
                     // check those existing items as to whether they need to be updated or removed
                     bool match = false;
                     foreach(AccomodationImageModel b in masterList) {
                          if(a == b) match = true; // match found
                     }
                     if(!match) rem.Add(a); else Update(a); // will be removed or updated
              }
    }
    // now remove unmatched items
    foreach(AccomodationImageModel a in rem) { masterList.Remove(a); }
    foreach(AccomodationImageModel a in newobj) { AddNew(a); }
    

    Note Update(AccomodationImageModel a) is your method to update a certain item and AddNew(AccomodationImageModel a) is your method of insterting a new item in the master list.

    Also as you may have noteiced removing from and inserting to the master list should be done after you have looped the master list!

    0 讨论(0)
  • 2020-12-15 06:26

    Simple Linq

    New

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

    To be deleted

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

    To be updated

    List<AccomodationImageModel> toBeUpdated = masterList.Where(c => compareList.Any(d => c.Id == d.Id)).ToList();
    
    0 讨论(0)
  • 2020-12-15 06:29

    Assuming that two models with the same Id are considered the same model, you can write a IEqualityComparer like this:

    public class AccommodationImageModelComparer : IEqualityComparer<AccommodationImageModel>
    {
        public bool Equals(AccommodationImageModel x, AccommodationImageModel y)
        {
            if(x == null && y == null)
               return true;
    
            return x.Id == y.Id;
        }
    
        public int GetHashCode(AccommodationImageModel model)
        {
            return model.Id.GetHashCode();
        }
    }
    

    You can then use Linq to get the lists that you want:

    var comparer = new AccommodationImageModelComparer();
    
    var newItems = compareList.Where (l => l.Id == 0).ToList();
    var toBeDeleted = masterList.Except(compareList, comparer).ToList();
    var toBeUpdated = masterList.Intersect(compareList, comparer).ToList();
    

    The first one just filters the items with an Id of 0, which are conisdered new. The second query returns the items in the masterList which are not in the compareList. The last query returns the items which are in both lists. This code compiles but is untested.

    0 讨论(0)
  • 2020-12-15 06:36
        ///If image is not in list then add the image to the list
    
    public void AddNew (List<AccomodationImageModel> masterList, AccomodationImageModel theImage)
    {
        masterList.Add(theImage);
    }
    
        /// If Image is in List then change listitem with new one
    
    public void Update (List<AccomodationImageModel> masterList, int OldOnesID, AccomodationImageModel theNew)
    {
        masterList[OldOnesID] = theNew;
    }
    
        /// If Image should delete then removes the image from list
    
    public void Delete (List<AccomodationImageModel> imgList, AccomodationImageModel theImageToDelete)
    {
        masterList.Remove(theImageToDelete);
    }
    
    
        /// this method checks the image state and do the work
    
    public void CheckState (List<AccommodationImageModel> masterList, AccomodationImageModel theImage, bool deleteIt)
    {
    
    
           for(int i = 0; i < masterList.Count; i++)
           {
    
             if (deleteIt)
             {
                Delete(masterList, theImage);
             }
    
             else
             {
               if(theImage.ID == 0)
               {
                 AddNew(masterList, theImage);
               }
    
               if(masterList[i].ID == theImage.ID)
               {
                 Update(masterList, i, theImage);
               }
           }
    }
    

    If you prefer use 2 lists as a master and Temporary list, then you can simply iterate your temporary list and each of templist item use CheckState method..

    Hope this helps..

    0 讨论(0)
提交回复
热议问题