Jerry Nixon @Work: Loving the Lambda SelectMany() method

Jerry Nixon on Windows

Tuesday, November 24, 2009

Loving the Lambda SelectMany() method

Lambda is simply a wonderful addition to C#. With it I can iterate and build expression trees from simple to complex. What's more, they perform so very well. Sometimes I prefer the "readable" LINQ syntax, but most often I fall back to the lambda superset.

Today, I stumbled on a new (new to me) method: SelectMany(). MSDN defines it cryptically like this: "Projects each element of a sequence to an IEnumerable<(Of <(T>)>) and combines the resulting sequences into one sequence."

Here's how you might use it:


Get the code here:

That does something VERY difficult. Each User has a nested list of offspring. Using SelectMany() we create a new result list with a distinct list of offspring that is derived from the outer User list.