Skip to content

Instantly share code, notes, and snippets.

@kcargile
Created August 5, 2012 14:24
Show Gist options
  • Save kcargile/3265109 to your computer and use it in GitHub Desktop.
Save kcargile/3265109 to your computer and use it in GitHub Desktop.
.NET compare two lists for equivalence using a predicate sort key.
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