Skip to content

Enumerable extensions

halcharger edited this page Apr 4, 2014 · 12 revisions

#####EnumerableExtensions.ContainsAll

Given:

var list = new[]{"one", "two", "three", "four"};
var items = new[]{"two", "three"};

We can check if list contains all of the values in items by writing:

list.ContainsAll(items)

We can also write:

list.ContainsAll("two", "three")

#####EnumerableExtensions.ContainsNone

Given:

var list = new[]{"one", "two", "three", "four"};
var items = new[]{"five", "six"};

We can check if list contains none of the values in items by writing:

list.ContainsNone(items)

We can also write:

list.ContainsNone("two", "three")

#####EnumerableExtensions.EmptyIfNull

Given a null IEnumerable<T>

List<string> strings = null;

Instead of:

var count = strings == null ? 0 : string.Count();

We can write:

var count = strings.EmptyIfNull.Count();

#####EnumerableExtensions.ForEach

Instead of:

foeach(var item in enumerable)
{
	DoSomething(item);
}

We can now write:

enumerable.ForEach(DoSomething);

Much more concise and expressive.

#####EnumerableExtensions.GetDuplicates

In the simple case, given an enumerable of simple types where we want to get duplicate values:

var values = new[]{"one,two,three,one,four,two"};

Instead of:

var duplicates = values.GroupBy(x => x).Where(x => x.Count() > 1).Select(x => x.Key);

We can write:

var duplicates = values.GetDuplicates();

Which will return the following IEnumerable<string> result:

new[]{"one", "two"}

In the more complex case where we want duplicates from an enumerable of a complex type, given:

public class User
{
    public string Name { get; set; }
    public string Email { get; set; }
}

var users = new[]
{
    new User{Name = "name1", Email = "[email protected]"}, 
    new User{Name = "name2", Email = "[email protected]"}, 
    new User{Name = "name3", Email = "[email protected]"}, 
    new User{Name = "name1", Email = "[email protected]"}, 
    new User{Name = "name2", Email = "[email protected]"}, 
    new User{Name = "name1", Email = "[email protected]"}, 
};

Instead of:

var duplicates = users.GroupBy(x => x.Name).Where(x => x.Count() > 1);

We can write:

var duplicates = users.GetDuplicates(x => x.Name);

Which will return an IEnumerable<IGrouping<string, User>> result so that we can actually see each individual duplicate item.

duplicates.Count() == 2
//name1 duplicates
duplicates.First().Count() == 3
//name2 duplicates
duplicates.Last().Count() == 2

#####EnumerableExtensions.IsNullOrEmpty

Given:

List<string> strings == null;

Or

List<string> strings = new List<string>();

Instead of writing:

var isNullOrEmpty = strings == null || string.Count() == 0;

We can write:

var isNullOrEmpty = strings.IsNullOrEmpty();

#####EnumerableExtensions.None

Instead of:

if (!enumerable.Any())
{
	//do something...
}

We can write:

if (enumerable.None())
{
	//do something...
}

Instead of:

if (!users.Any(u => u.Username == "user123"))
{
	//do something...
}

We can write:

if (users.None(u => u.Username == "user123"))
{
	//do something...
}
Clone this wiki locally