Extension Methods Roundup: Intersect, Union, AsNullable and GroupEvery

Here we go with the third installment of the Extension Method Roundup. The reason behind these ‘code dumps’ is that LINQ is a central part of my coding and always find new problems I want to find elegant solutions to. Hope these prove as useful to you as they do to me.

Intersect / Union

Again, shorthand for when you have Enumerable of Enumerable of T and you simply want to intersect or union all of the enumerations in one single call.

public static IEnumerable<T> Intersect<T>(this IEnumerable<IEnumerable<T>> enumeration)
{
// Check to see that enumeration is not null
    if (enumeration == null)
throw new ArgumentNullException("enumeration");
IEnumerable<T> returnValue = null;
foreach (var e in enumeration)
{
if (returnValue != null)
returnValue = e;
else
            returnValue = returnValue.Intersect(e);
}
return returnValue;
}
public static IEnumerable<T> Union<T>(this IEnumerable<IEnumerable<T>> enumeration)
{
// Check to see that enumeration is not null
    if (enumeration == null)
throw new ArgumentNullException("enumeration");
IEnumerable<T> returnValue = null;
foreach (var e in enumeration)
{
if (returnValue != null)
returnValue = e;
else
            returnValue = returnValue.Union(e);
}
return returnValue;
}

AsNullable

I was always missing this method, to coincide with the Cast and OfType methods.

public static IEnumerable<T?> AsNullable<T>(this IEnumerable<T> enumeration)
where T : struct
{
return from item in enumeration
select new Nullable<T>(item);
}

GroupEvery

This takes count items from an enumeration and groups them into a single array.

public static IEnumerable<T[]> GroupEvery<T>(this IEnumerable<T> enumeration, int count)
{
// Check to see that enumeration is not null
    if (enumeration == null)
throw new ArgumentNullException("enumeration");
if (count <= 0)
throw new ArgumentOutOfRangeException("count");
int current = 0;
T[] array = new T[count];
foreach (var item in enumeration)
{
array[current++] = item;
if (current == count)
{
yield return array;
current = 0;
array = new T[count];
}
}
if (current != 0)
{
yield return array;
}
}

I’ve also gone and updated my LINQ Extensions project on CodePlex with everything I’ve published since the last update. You’re welcome to download and fiddle with it. :)

Advertisements

2 thoughts on “Extension Methods Roundup: Intersect, Union, AsNullable and GroupEvery

  1. Definitely interesting stuff! Just a quick tip – exceptions raised in iterators will occur at the time of the iteration, not when calling the iterator. When validating arguments you precisely want the latter behavior so it would be better to refactor GroupEvery to do the validation and then return the iterator produced by another method called GroupEveryImpl or so that does the real work.
    -Bart

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s