Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Select instead of SelectMany returns a list of lists, not a flat list.
✗ Incorrect
The SelectMany method flattens a collection of collections into a single collection.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Select only returns nested lists, so Where filters lists, not numbers.
✗ Incorrect
SelectMany flattens the list, then Where filters numbers greater than 2.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Select causes flatWords to be a list of arrays, not strings.
✗ Incorrect
SelectMany flattens the array of arrays into a single list of strings.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Select instead of SelectMany keeps nested lists.
Using Count instead of Length on strings causes errors.
✗ Incorrect
SelectMany flattens the list, then Length gets each word's length.
5fill in blank
hardFill 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'
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.
✗ Incorrect
SelectMany flattens the list, Length filters words longer than 3, and ToUpper() converts words to uppercase.