Created
August 5, 2012 14:24
-
-
Save kcargile/3265109 to your computer and use it in GitHub Desktop.
.NET compare two lists for equivalence using a predicate sort key.
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Extensions | |
{ | |
public static class IEnumerableExtensions | |
{ | |
// e.g. bool sameList = list1.Equals(list2, entity => entity.Id) | |
public static bool Equals<T, TKey>(this IEnumerable<T> source, IEnumerable<T> listToCompare, Func<T, TKey> predicate) where T : class | |
{ | |
return ((null == source || null == listToCompare) ? source == listToCompare : ((source.Equals(listToCompare)) | (source.OrderBy(predicate).SequenceEqual(listToCompare.OrderBy(predicate))))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment