Blog - Title

Chunking a Collection into Groups of Three

Chunking a Collection into Groups of Three

  • Comments 4

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

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC

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.

Leave a Comment
  • Please add 5 and 2 and type the answer here:
  • Post
  • Excellent. Didn't know you could do that.

  • 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

  • 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

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

Page 1 of 1 (4 items)