SelectMany helps you take many small lists inside a big list and turn them into one big list. It makes it easy to work with all items together.
SelectMany for flattening in C Sharp (C#)
var flatList = bigList.SelectMany(innerList => innerList);
SelectMany takes a function that returns a collection for each item.
It then merges all those collections into one single collection.
var numbers = new List<List<int>> { new List<int> {1, 2}, new List<int> {3, 4} }; var flat = numbers.SelectMany(x => x);
var words = new List<string[]> { new string[] {"apple", "banana"}, new string[] {"cherry"} }; var allWords = words.SelectMany(arr => arr);
var people = new List<Person> { new Person { Name = "Alice", Pets = new List<string>{"Dog", "Cat"} }, new Person { Name = "Bob", Pets = new List<string>{"Fish"} } }; var allPets = people.SelectMany(p => p.Pets);
This program creates classrooms with students and uses SelectMany to get one list of all students. Then it prints each student.
using System; using System.Collections.Generic; using System.Linq; class Program { class Classroom { public string Name { get; set; } public List<string> Students { get; set; } } static void Main() { var classrooms = new List<Classroom> { new Classroom { Name = "Class A", Students = new List<string>{"John", "Jane"} }, new Classroom { Name = "Class B", Students = new List<string>{"Tom", "Lucy"} } }; var allStudents = classrooms.SelectMany(c => c.Students); foreach (var student in allStudents) { Console.WriteLine(student); } } }
SelectMany is great for flattening nested collections into one.
If you use Select instead, you get a list of lists, not a flat list.
Remember to include using System.Linq; to use SelectMany.
SelectMany turns many small lists inside a big list into one big list.
It helps when you want to work with all items together instead of nested groups.
Use it with a function that picks the inner list from each item.
Practice
SelectMany method do in C# when used on a list of lists?Solution
Step 1: Understand the structure of nested lists
A list of lists means you have many small lists inside one big list.Step 2: Role of SelectMany
SelectMany takes each small list and combines all their elements into one big flat list.Final Answer:
It flattens the nested lists into a single list containing all elements. -> Option CQuick Check:
SelectMany flattens nested lists = B [OK]
- Thinking SelectMany sorts or filters instead of flattening
- Confusing SelectMany with Select which keeps nested structure
- Assuming SelectMany creates dictionaries
List numbers using SelectMany?Solution
Step 1: Understand SelectMany syntax
SelectMany expects a function that returns an inner collection to flatten.Step 2: Check each option
var flat = numbers.SelectMany(x => x); returns each inner list as is, which is correct. var flat = numbers.Select(x => x); uses Select, not flattening. Options C and D return wrong types (string and int), not collections.Final Answer:
var flat = numbers.SelectMany(x => x); -> Option AQuick Check:
SelectMany with identity function flattens = A [OK]
- Using Select instead of SelectMany
- Returning non-collections inside SelectMany
- Trying to flatten by counting elements
var data = new List {
new List<int> {1, 2},
new List<int> {3, 4},
new List<int> {5}
};
var result = data.SelectMany(x => x).ToList();
Console.WriteLine(string.Join(",", result));Solution
Step 1: Understand SelectMany flattening
SelectMany flattens all inner lists into one list: [1,2,3,4,5].Step 2: Output formatting
string.Join with "," joins elements with commas, so output is "1,2,3,4,5".Final Answer:
1,2,3,4,5 -> Option DQuick Check:
Flattened list joined by commas = D [OK]
- Expecting nested list output instead of flat
- Confusing ToString output of list object
- Missing commas in output
List words = new() {
new List<string> {"apple", "banana"},
new List<string> {"cat", "dog"}
};
var flatWords = words.SelectMany(w => w.Length).ToList();Solution
Step 1: Analyze the lambda inside SelectMany
The lambda uses w => w.Length, which returns an int, not a collection.Step 2: Understand SelectMany requirements
SelectMany expects a function returning a collection to flatten, not a single int value.Final Answer:
Using w.Length returns int, not a collection, causing a compile error. -> Option BQuick Check:
SelectMany needs collections, not int = C [OK]
- Returning single values instead of collections in SelectMany
- Confusing Length property with collection
- Assuming ToList() causes error
SelectMany to get a list of all grades above 80 from all students?
class Student {
public string Name { get; set; }
public List<int> Grades { get; set; }
}
List<Student> students = new() {
new Student { Name = "Anna", Grades = new List<int> { 90, 70 } },
new Student { Name = "Ben", Grades = new List<int> { 85, 60 } }
};
// Fill in the blank to get all grades > 80
var highGrades = students.SelectMany(__________).ToList();Solution
Step 1: Understand the goal
We want all grades above 80 from all students combined.Step 2: Use SelectMany with filtering inside
s => s.Grades.Where(g => g > 80) filters grades > 80 inside SelectMany, flattening only those grades.Step 3: Why not others?
s => s.Grades flattens all grades, including low ones like 70 and 60. Options C and D return wrong types (string and int).Final Answer:
s => s.Grades.Where(g => g > 80) -> Option AQuick Check:
SelectMany with filtered inner lists = A [OK]
- Flattening all grades without filtering
- Returning non-collections in SelectMany
- Using wrong property like Name or Count
Ready to prove it?
Pass the test and this pattern moves unknown
