Suggestion for Merge in List

Suggestion for Merge in List

Old forum URL: forums.lhotka.net/forums/t/7280.aspx


Jack posted on Wednesday, July 15, 2009

I have found I am often wanting to merge some data into a cached list and have been writing the same code over and over.  Any chance for a generic Merge/MergeList method?  my ToObservableCollection() is an extension method I plugged in for Silverlight.

Seems to me this should be able to be made generic fairly simply?  This example reads in a list, joins to the parent on the PKey to find the replacements, removes them, and then essentially appends the new list to the old one.

public void Merge(IEnumerable<ObjectInfo> updatedList)
        {

            RaiseListChangedEvents = false;
            IsReadOnly = false;

            // identify all the duplicate records
            var existingList = from currentData in this
                               join updatedData in updatedList
                                   on currentData.ObjectId equals updatedData.ObjectId
                               select currentData;

            // look through the list and remove any duplicates
            foreach (var updatedObject in existingList.ToObservableCollection()) Remove(updatedObject);

            AddRange(updatedList);

            IsReadOnly = true;
            RaiseListChangedEvents = true;   
        }

Copyright (c) Marimer LLC