Skip to content

Instantly share code, notes, and snippets.

@kpko
Last active August 29, 2015 14:21
Show Gist options
  • Save kpko/693655e7e5a05ad66ad9 to your computer and use it in GitHub Desktop.
Save kpko/693655e7e5a05ad66ad9 to your computer and use it in GitHub Desktop.
.NET: Merge statement for lists
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