Created
March 25, 2015 19:24
-
-
Save KerryRitter/729665eb346c4b6bc5cb to your computer and use it in GitHub Desktop.
EntityRelationshipMap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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