Created
April 19, 2010 12:43
-
-
Save tathamoddie/371001 to your computer and use it in GitHub Desktop.
Enumerable.SetEqual<T> extension
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
/// <summary> | |
/// An order independent version of <see cref="Enumerable.SequenceEqual{TSource}(System.Collections.Generic.IEnumerable{TSource},System.Collections.Generic.IEnumerable{TSource})"/>. | |
/// </summary> | |
internal static bool SetEqual<T>(this IEnumerable<T> x, IEnumerable<T> y) | |
{ | |
if (x == null) throw new ArgumentNullException("x"); | |
if (y == null) throw new ArgumentNullException("y"); | |
var objectsInX = x.ToList(); | |
var objectsInY = y.ToList(); | |
if (objectsInX.Count() != objectsInY.Count()) | |
return false; | |
foreach (var objectInY in objectsInY) | |
{ | |
if (!objectsInX.Contains(objectInY)) | |
return false; | |
objectsInX.Remove(objectInY); | |
} | |
return objectsInX.Empty(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment