Last active
August 29, 2015 14:21
-
-
Save kpko/693655e7e5a05ad66ad9 to your computer and use it in GitHub Desktop.
.NET: Merge statement for lists
This file contains hidden or 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 Merge<T>(this IEnumerable<T> from, IEnumerable<T> into, | |
Action<T> newItem = null, Action<T> existingItem = null, Action<T> removedItem = null, | |
IEqualityComparer<T> comparer = null) | |
{ | |
if (newItem != null) | |
{ | |
var newItems = from.Except(into, comparer).ToList(); | |
foreach (var item in newItems) | |
{ | |
newItem(item); | |
} | |
} | |
if (existingItem != null) | |
{ | |
var existingItems = from.Intersect(into, comparer).ToList(); | |
foreach (var item in existingItems) | |
{ | |
existingItem(item); | |
} | |
} | |
if (removedItem != null) | |
{ | |
var removedItems = into.Except(from, comparer).ToList(); | |
foreach (var item in removedItems) | |
{ | |
removedItem(item); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment