codethinked (kōdthĭngked) adj. To be consumed by or obsessed with code.

A Visual Look At The LINQ SelectMany Operator

If you want to learn all about LINQ, first go check out my series on TekPub and then come back here!

So, I blogged a while ago about the LINQ SelectMany operator. The LINQ SelectMany operator is one of the most useful, misunderstood, and underused operators in your LINQ repertoire. In my previous post I gave you a decent idea of what you can do with the LINQ SelectMany operator, but I'm not quite sure that I did a very good job at really showing you how it works. In this post, I want to give you a more visual explanation of the LINQ SelectMany operator, and what it can do for you.

A Visual Explanation of SelectMany

As I said in my previous post about the SelectMany operator, MSDN describes it like this: "Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence." Again, once you are familiar with SelectMany, this is a great explanation, but it can still be a bit hard to visualize. So, let's see...

1) it takes a sequence:

image

2) And then it iterates over each element in this sequence:

 image

3) While it is iterating, it projects each element into an IEnumerable<T>:

 image

4) And then flattens the resulting IEnumerables back into a single sequence:

 image

Pretty easy to see  now, right? The SelectMany operator allows us to produce a single sequence with 1 to n items for each item in the original sequence. It really allows us to sort-of multiply each item in a sequence, or at least project it into multiple items. If we did something like this with Select then we would end up producing a sequence with multiple sub-sequences, like this:

 image

While this might be useful, it is not exactly what we want in this case. We want a single contiguous sequence which we can then operate on using other LINQ operators.

Different Kinds Of Projections

Remember that all you have to do is get a list from each item in a list. You can do this by accessing a list that is on each item, like this:

image

Or you could do it by generating a list, for example, what if each item was an integer, we could generate a range based on each number:

image

Pretty cool, and powerful. It also lets us chain calls to SelectMany, like this:

image

So basically, it let's you continue to generate n number of sequences, and then combine them all back together. Since each call to SelectMany generates a single sequence, then you can call SelectMany on that resulting sequence and continue to do so as many times as you want.

Summary

While most of the operators in LINQ let you get one output element for each element you have in your sequence, or they let you filter out elements, but SelectMany is the only operator that lets you produce n output elements for each element in your input sequence. This fact opens up all kinds of possibilities with LINQ that otherwise wouldn't be available to you. I hope this helps you out!

Comments

pingback

Pingback from blog.cwa.me.uk

The Morning Brew - Chris Alcock  » The Morning Brew #558

blog.cwa.me.uk

March 12. 2010 03:35

trackback

A Visual Look At The LINQ SelectMany Operator

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

March 12. 2010 10:03

Dave Ward

That last example is a really good one.  Too many of the advanced LINQ examples are so esoteric that it's hard to imagine applying them, but I can see myself needing that projection and probably would have done it a messier way than this before.

Dave Ward

March 12. 2010 10:20

United States
Camal

Hey Justin,
which tool do you use for illustration ?

Thanks

Camal

March 12. 2010 10:42

Germany
Justin Etheredge

@Camal Just Microsoft Powerpoint 2007.

Justin Etheredge

March 12. 2010 10:45

United States
trackback

Social comments and analytics for this post

This post was mentioned on Twitter by adrianhara: nice visual representation of Linq's SelectMany: http://bit.ly/afIcZR

uberVU - social comments

March 12. 2010 16:28

Tiendq

Thanks, your post is really useful, it helps me to understand SelectMany better and easier. But I think it'd be more useful if you provide some sample codes along with nice graphics.

Tiendq

March 12. 2010 21:44

Vietnam
Ian

Great diagrams!

Ian

March 13. 2010 19:56

United States
pingback

Pingback from blog.abodit.com

Twitter links for Week beginning March 8th «  Ian Mercer

blog.abodit.com

March 13. 2010 19:57

pingback

Pingback from octofinder.com

A Visual Look At The LINQ SelectMany Operator [ CodeThinked ]

octofinder.com

March 15. 2010 02:14

pingback

Pingback from tamm79.de

Markus Tamm  » Blog Archive   » Links 15.03.2010

tamm79.de

March 15. 2010 04:55

Jon Fuller

Cool!  Thanks man!

I'd been doing

customers.Map(x => x.Orders).Flatten()

(Map is my version of 'Select')

This will allow me to combine it into a simple one step process rather than 2... excellent.

Thanks again!

Jon Fuller

March 15. 2010 08:45

United States
trackback

Daily tech links for .net and related technologies - Mar 18-21, 2010

Daily tech links for .net and related technologies - Mar 18-21, 2010 Web Development TDD kata for ASP

Sanjeev Agarwal

March 18. 2010 07:29

Hatim

I was pretty familiar with the selectMany operator but Love the diagrams Smile and a pretty good explanation!

Hatim

March 20. 2010 05:46

Morocco
sisirak

A picture is worth a thousand words
Thank you

Sisirak

sisirak

April 1. 2010 17:22

U.A.E.
Matt Lashley

Nice job!  Love the visual representations.  They really "speak to me" Smile

Matt Lashley

May 13. 2010 16:32

United States
Nick Olsen

Awesome post! Thanks!

Nick Olsen

July 26. 2010 19:16

United States

Add Comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading