0
0
C Sharp (C#)programming~10 mins

SelectMany for flattening in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SelectMany for flattening
Start with collection of collections
Apply SelectMany
Flatten inner collections
Create single flat collection
Use or output flat collection
SelectMany takes a collection of collections and flattens it into one single collection by combining all inner elements.
Execution Sample
C Sharp (C#)
var numbers = new List<List<int>> {
    new List<int> {1, 2},
    new List<int> {3, 4},
    new List<int> {5}
};
var flat = numbers.SelectMany(x => x).ToList();
This code flattens a list of lists of integers into one list of integers.
Execution Table
StepCurrent Outer ElementInner ElementsActionFlat Collection State
1[1, 2]1, 2Add 1, 2 to flat collection[1, 2]
2[3, 4]3, 4Add 3, 4 to flat collection[1, 2, 3, 4]
3[5]5Add 5 to flat collection[1, 2, 3, 4, 5]
ExitNo more outer elements-Stop flattening[1, 2, 3, 4, 5]
💡 All inner lists processed, flat collection complete
Variable Tracker
VariableStartAfter 1After 2After 3Final
flatempty[1, 2][1, 2, 3, 4][1, 2, 3, 4, 5][1, 2, 3, 4, 5]
Key Moments - 2 Insights
Why does SelectMany flatten the inner lists instead of keeping them nested?
SelectMany takes each inner collection and adds its elements directly to the result, so the output is a single flat list, as shown in execution_table rows 1-3.
What happens if the inner collections are empty?
If an inner collection is empty, SelectMany adds nothing for that step, so the flat collection stays the same, similar to skipping that row in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the flat collection after step 2?
A[1, 2]
B[1, 2, 3, 4]
C[3, 4]
D[1, 2, 3]
💡 Hint
Check the 'Flat Collection State' column at step 2 in the execution_table.
At which step does the flat collection first contain the number 5?
AStep 1
BStep 2
CStep 3
DExit step
💡 Hint
Look at the 'Inner Elements' and 'Flat Collection State' columns in the execution_table.
If the second inner list was empty, how would the flat collection look after step 3?
A[1, 2, 5]
B[1, 2, 3, 4, 5]
C[5]
D[1, 2]
💡 Hint
Consider that an empty inner list adds no elements, so the flat collection skips those.
Concept Snapshot
SelectMany flattens collections of collections into one collection.
Syntax: collection.SelectMany(inner => inner)
It takes each inner collection and adds its elements to a single flat list.
Useful to simplify nested lists into one list.
Remember: output has no nested structure after flattening.
Full Transcript
This visual execution shows how SelectMany works in C#. We start with a list of lists of integers. SelectMany takes each inner list and adds its elements to a new flat list. Step by step, the flat list grows as elements from each inner list are added. When all inner lists are processed, the flat list contains all elements in one sequence. This helps beginners see how nested collections become flat using SelectMany.