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

SelectMany for flattening in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does SelectMany do in LINQ?

SelectMany takes a collection of collections and flattens it into a single collection by combining all inner collections into one.

Click to reveal answer
beginner
How is SelectMany different from Select?

Select projects each element into a new form but keeps the nested collections, while SelectMany flattens those nested collections into one sequence.

Click to reveal answer
beginner
Example: What is the output of this code?<br>
var numbers = new List<List<int>> { new() {1, 2}, new() {3, 4} };
var flat = numbers.SelectMany(x => x);
foreach(var n in flat) Console.Write(n + " ");

The output is: 1 2 3 4

Because SelectMany flattens the list of lists into a single list of numbers.

Click to reveal answer
beginner
Can SelectMany be used with arrays or only lists?

SelectMany works with any IEnumerable<T>, so it works with arrays, lists, and other collections.

Click to reveal answer
beginner
Why is SelectMany useful in real life?

It helps when you have nested data like a list of orders where each order has many items, and you want to work with all items in one list.

Click to reveal answer
What does SelectMany do in LINQ?
AGroups elements by a key
BFilters elements based on a condition
CSorts elements in ascending order
DFlattens nested collections into a single collection
Which method keeps nested collections without flattening?
ASelectMany
BWhere
CSelect
DOrderBy
Given var data = new List<List<int>> { new() {1,2}, new() {3,4} };, what does data.SelectMany(x => x) return?
AA flat list: 1, 2, 3, 4
BA list of lists
CAn empty list
DA list with two elements
Can SelectMany be used on arrays?
AYes, on any IEnumerable&lt;T&gt;
BNo, only on lists
COnly on dictionaries
DOnly on strings
Why use SelectMany when working with nested data?
ATo remove duplicates
BTo flatten nested collections for easier processing
CTo sort nested collections
DTo convert collections to strings
Explain in your own words what SelectMany does and give a simple example.
Think about turning many small boxes into one big box.
You got /3 concepts.
    Describe a real-life scenario where using SelectMany would make your code simpler.
    Imagine you have many shopping bags and want to see all items at once.
    You got /3 concepts.