Welcome to MSDN Blogs Sign in | Join | Help
Chunking a Collection into Groups of Three

[Blog Map] 

Raw data for a LINQ query doesn’t always come in the form you want.  Recently, I had some data like this:

string[] source = new [] {

    "EW",

    "Eric",

    "8:00",

    "DW",

    "Dave",

    "9:00",

    "JA",

    "Jim",

    "8:00"

};

 

You want to transform the above collection of strings into a collection of anonymous objects that are easier to work on, so you need to write a query that operates on groups of three.  To solve this problem, you can take advantage that the Select extension method has an overload that passes an index number to the selection function:

string[] source = new [] {

    "EW",

    "Eric",

    "8:00",

    "DW",

    "Dave",

    "9:00",

    "JA",

    "Jim",

    "8:00"

};

var people = source

    .Select

    (

        (s, i) =>

            new

            {

                Value = s,

                Chunk = i / 3

            }

    )

    .GroupBy(x => x.Chunk)

    .Select

    (

        g =>

            new

            {

                Initials = g.First().Value,

                Name = g.Skip(1).First().Value,

                Time = g.Skip(2).First().Value

            }

    );

foreach (var p in people)

    Console.WriteLine(p);

 

This produces the following output:

{ Initials = EW, Name = Eric, Time = 8:00 }

{ Initials = DW, Name = Dave, Time = 9:00 }

{ Initials = JA, Name = Jim, Time = 8:00 }

 

This new collection is a whole lot easier to work with.
Posted: Tuesday, August 19, 2008 1:27 PM by EricWhite

Comments

Doug said:

Excellent. Didn't know you could do that.

# August 19, 2008 10:23 AM

Samuel Jack said:

Eric,

 I've also been thinking about the problem of chunking a sequence. I've written up an alternative method, using an iterator, in this post:

http://blog.functionalfun.net/2008/08/anyone-for-slice-of-linq.html

# August 22, 2008 9:19 AM

EricWhite said:

Samual,

Your approach using an interator is fine too.  It has an advantage - the approach that I showed creates more short-lived objects on the heap.  Sometimes I am writing ad-hoq LINQ transforms for one reason or another, in which case I would use the above approach.  If I were making a library that would be used by many people, I would optimize using an approach similar to yours.

-Eric

# August 22, 2008 5:10 PM

Eric White's Blog said:

Transforming Open XML documents using XSLT is an interesting scenario, but before we can do so, we need

# September 29, 2008 9:07 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker