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

SelectMany for flattening in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to flatten the list of lists using LINQ.

C Sharp (C#)
var numbers = new List<List<int>> { new List<int> {1, 2}, new List<int> {3, 4} };
var flat = numbers.[1](x => x).ToList();
Drag options to blanks, or click blank then click option'
AOrderBy
BSelect
CSelectMany
DWhere
Attempts:
3 left
💡 Hint
Common Mistakes
Using Select instead of SelectMany returns a list of lists, not a flat list.
2fill in blank
medium

Complete the code to flatten and filter numbers greater than 2.

C Sharp (C#)
var numbers = new List<List<int>> { new List<int> {1, 2}, new List<int> {3, 4} };
var result = numbers.[1](x => x).Where(n => n > 2).ToList();
Drag options to blanks, or click blank then click option'
AOrderBy
BSelect
CWhere
DSelectMany
Attempts:
3 left
💡 Hint
Common Mistakes
Using Select only returns nested lists, so Where filters lists, not numbers.
3fill in blank
hard

Fix the error in the code to flatten the list of arrays.

C Sharp (C#)
string[][] words = { new string[] {"apple", "banana"}, new string[] {"cherry"} };
var flatWords = words.[1](w => w).ToList();
Drag options to blanks, or click blank then click option'
ASelectMany
BSelect
CWhere
DOrderBy
Attempts:
3 left
💡 Hint
Common Mistakes
Using Select causes flatWords to be a list of arrays, not strings.
4fill in blank
hard

Fill both blanks to flatten and select the length of each word.

C Sharp (C#)
var words = new List<List<string>> { new List<string> {"cat", "dog"}, new List<string> {"bird"} };
var lengths = words.[1](w => w).Select(word => word.[2]).ToList();
Drag options to blanks, or click blank then click option'
ASelectMany
BSelect
CLength
DCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using Select instead of SelectMany keeps nested lists.
Using Count instead of Length on strings causes errors.
5fill in blank
hard

Fill all three blanks to flatten, filter words longer than 3, and convert to uppercase.

C Sharp (C#)
var words = new List<List<string>> { new List<string> {"cat", "dog"}, new List<string> {"bird", "fish"} };
var result = words.[1](w => w).Where(word => word.[2] > 3).Select(word => word.[3]()).ToList();
Drag options to blanks, or click blank then click option'
ASelect
BLength
CToUpper
DSelectMany
Attempts:
3 left
💡 Hint
Common Mistakes
Using Select instead of SelectMany keeps nested lists.
Using Count instead of Length on strings causes errors.
Forgetting parentheses on ToUpper causes errors.