Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

Omer van Kloeten's Facebook profile

Omer has been professionally developing applications over the past 8 years, both at the IDF’s IT corps and later at the Sela Technology Center, but has had the programming bug ever since he can remember himself.
As a senior developer at NuConomy, a leading web analytics and advertising startup, he leads a wide range of technologies for its flagship products.

Get Firefox


powered by Dapper 

.NET Resources

Articles :: CodeDom

Articles :: nGineer

Culture

Projects

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. :)

Comments

Dew Drop - August 1, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - August 1, 2008 | Alvin Ashcraft's Morning Dew

# August 1, 2008 9:52 AM

Jason Haley said:

# August 1, 2008 10:26 AM

.NET Utility Libraries Galore « HSI Developer Blog said:

Pingback from  .NET Utility Libraries Galore &laquo; HSI Developer Blog

# August 1, 2008 11:50 AM

Bart said:

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

# August 24, 2008 2:31 AM

Omer van Kloeten said:

Hum, you're right. I'll correct it.

# August 24, 2008 2:13 PM

Throw Before You Yield - Omer van Kloeten's .NET Zen said:

Pingback from  Throw Before You Yield - Omer van Kloeten's .NET Zen

# August 25, 2008 3:23 PM

Throw Before You Yield - ????????.???? said:

Pingback from  Throw Before You Yield - ????????.????

# September 18, 2008 4:24 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)