Easier manipulation with IEnumerable objects

Useful extension methods for working with lists, arrays, dictionaries...

Working with any collection which implements IEnumerable is pretty easy in C#, plus if you are using LINQ you can easily manipulate with collection data.

Recently I was doing some filtering with some in-memory collections and I had to check whether one collection contains same elements as some other collection with elements of the same type. At first sight this looks pretty simple, but first problem is that collection elements might not be in the same order in both collection.

Different order can be overcome with simple sort using LINQ, but still you have to compare first length of collections and than ordered elements

This does not sound so difficult, but the thing is it can be dome within a single line.

 The trick is to treat your collections as HashSets.

HashSet class provides this functionality out of the box. To make it even more easy to use, I created three extension methods which are applicable to any IEnumerable collection of generic type.

Compare if collection elements are the same regardless of collection order

public static bool IsEqual<T>(this IEnumerable<T> Compare, IEnumerable<T> CompareTo)
{
    return new HashSet<T>(Compare ?? new List<T>()).SetEquals(CompareTo ?? new List<T>());
}
    

Check if collection contains other collection of same type

public static bool Contains<T>(this IEnumerable<T> Superset, IEnumerable<T> Subset)
{
    return new HashSet<T>(Superset ?? new List<T>()).IsSupersetOf(Subset ?? new List<T>());
}
    

Check if collection is contained in other collection

public static bool ContainedIn<T>(this IEnumerable<T> Subset, IEnumerable<T> Superset)
{
    return new HashSet<T>(Subset ?? new List<T>()).IsSubsetOf(Superset ?? new List<T>());
}
    

These extension methods could be really useful in cases you need to compare two collections such as filtering which was my case.

One more thing using these extensions is that you do not need to perform NULL check as extension method will o that for you. This way number of possible exceptions is minimized.

Extension methods enable applying the check logic to any collection that implements IEnumerable, so you can easily to the following

var list1 = new int[] { 1, 2, 3, 4 };
            var list2 = new List<int>() { 4, 1, 3, 2 };

            var result = list2.IsEqual(list1);
    

References

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform including ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

JavaScript

read more

SQL/T-SQL

read more

Umbraco CMS

read more

PowerShell

read more

Comments for this article