Linq – ‘Chunking’ Enumerable Data With New Extension Method – ‘SelectChunk’

A couple of times now I've needed to lazy-enumerate a large collection, but in ‘chunks’ – or to put it in another way, I've needed to bring data back from the collection in bite-sized batches.

There currently isn't a Linq extension to support this (afaik), so I wrote one. It preserves the lazy-enumerating characteristics of most other Linq extensions, and seems to work very well.

public static IEnumerable<IEnumerable<TResult>> SelectChunk<TSource, TResult>(
        this IEnumerable<TSource> source, Func<TSource, TResult> selector, int chunkSize)
    {
        IEnumerator<TSource> enumerator = source.GetEnumerator();
        while(true)
        {
            if (!enumerator.MoveNext())
                break;
            var resultArray = new TResult[chunkSize];
            for (int i = 0; i < chunkSize; i++)
            {
                resultArray[i] = selector(enumerator.Current);
                if (i == chunkSize-1 || !enumerator.MoveNext())
                    break;
            }
            yield return resultArray;
        } 
    }

And here is an example of its usage:

IEnumerable<string> data = new[] {"John", "Doe", "Male", "Jane", "Smith", "Female"};

var people =
    data.SelectChunk(s => s, 3)
        .Select(s => new
            {
                FirstName = s.ElementAt(0), 
                LastName = s.ElementAt(1), 
                Sex = s.ElementAt(2)
            });

Comments (1) -

Bhushan Poojary
Bhushan Poojary
2/18/2012 7:28:28 AM #

Nice post.Visit <a href="extensionmethodkit.codeplex.com/wikipage Extension Methods</a> or visit <a href="extensionmethodkit.codeplex.com/">Extension Method Kit</a> for full set.

Pingbacks and trackbacks (2)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About the author

I am a senior .NET contract (freelance) software developer specialising in WPF and WinRT application development with C#, F#, C++. I mainly work in the Investment Bnking industry building performant and robust user interfaces for front office and middle office systems

RecentComments

Comment RSS

Month List