Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

Follow publication

Understanding Select vs SelectMany in LINQ

Jay Krishna Reddy
Level Up Coding
Published in
3 min readMar 15, 2025

--

LINQ (Language Integrated Query) is a powerful feature in .NET that allows developers to query collections of data in a streamlined and expressive manner. Among its many operators, Select and SelectMany often confuse developers due to their seemingly similar functionality. This article will demystify these two methods, explain their differences, and help you decide when to use each one.

The Basics: Select vs. SelectMany

Select: Preserve Structure

The Select method transforms each element in a sequence into a new form while preserving the original collection’s structure. This means the result of a Select operation is a sequence of sequences (a collection of collections).

Example:

Imagine we have a collection of bouquets, each containing a list of flowers:

var bouquets = new List<List<string>>
{
new List<string> { "Rose", "Tulip", "Lily" },
new List<string> { "Daisy", "Sunflower" },
new List<string> { "Orchid" }
};

var flowers = bouquets.Select(bouquet => bouquet);

The Select operation projects each bouquet as it is, resulting in a sequence of bouquets:

List<List<string>>
[
["Rose", "Tulip", "Lily"],
["Daisy", "Sunflower"],
["Orchid"]
]

You still have nested collections and would need to iterate through them to access individual flowers.

SelectMany: Flatten the Structure

The SelectMany method, on the other hand, flattens the collections into a single sequence. This operation is ideal when you want to work with individual elements rather than nested collections.

Example:

Using the same bouquets collection:

var allFlowers = bouquets.SelectMany(bouquet => bouquet);

The result of SelectMany is a flattened list of all flowers:

["Rose", "Tulip", "Lily", "Daisy", "Sunflower", "Orchid"]

You no longer need to iterate through nested collections; you can directly work with the individual elements.

What’s Happening Under the Hood?

With Select:

Each element in the original collection is transformed into a new form, but the…

--

--

No responses yet

Write a response