Skip to content

Instantly share code, notes, and snippets.

@KerryRitter
Created March 25, 2015 19:24
Show Gist options
  • Save KerryRitter/729665eb346c4b6bc5cb to your computer and use it in GitHub Desktop.
Save KerryRitter/729665eb346c4b6bc5cb to your computer and use it in GitHub Desktop.
EntityRelationshipMap
public static void EntityRelationshipMap<T, K>(this ICollection<T> startingCollection, ICollection<T> newCollection,
Func<T, K> selector )
{
var newItemCompareValues = newCollection.Select(selector);
var existingItemCompareValues = startingCollection.Select(selector);
var alternativeTitlesToRemove = startingCollection.Where(at => !newItemCompareValues.Contains(selector(at)));
var alternativeTitlesToAdd = newCollection.Where(at => !existingItemCompareValues.Contains(selector(at)));
foreach (var alternativeTitleToRemove in alternativeTitlesToRemove)
{
startingCollection.Remove(alternativeTitleToRemove);
}
foreach (var alternativeTitleToAdd in alternativeTitlesToAdd)
{
startingCollection.Add(alternativeTitleToAdd);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment