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

GroupBy operation 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 group numbers by their remainder when divided by 3.

C Sharp (C#)
var groups = numbers.[1](n => n % 3);
Drag options to blanks, or click blank then click option'
ASelect
BWhere
CGroupBy
DOrderBy
Attempts:
3 left
💡 Hint
Common Mistakes
Using Select instead of GroupBy will transform elements but not group them.
Where filters elements but does not group.
OrderBy sorts elements but does not group.
2fill in blank
medium

Complete the code to print each group key and its elements.

C Sharp (C#)
foreach (var group in groups) {
    Console.WriteLine($"Key: {group.[1]");
    foreach (var num in group) {
        Console.WriteLine(num);
    }
}
Drag options to blanks, or click blank then click option'
ACount
BKey
CToList
DElementAt
Attempts:
3 left
💡 Hint
Common Mistakes
Using Count instead of Key will print the number of elements, not the key.
ToList is a method, not a property, and does not represent the key.
ElementAt requires an index and is not the key.
3fill in blank
hard

Fix the error in the code to group words by their first letter.

C Sharp (C#)
var groupedWords = words.[1](w => w[0]);
Drag options to blanks, or click blank then click option'
AGroupBy
BWhere
CSelect
DOrderBy
Attempts:
3 left
💡 Hint
Common Mistakes
Using Select or Where instead of GroupBy will not create groups.
OrderBy sorts but does not group.
4fill in blank
hard

Fill both blanks to create a dictionary from groups with key and count of elements.

C Sharp (C#)
var dict = groupedWords.ToDictionary(g => g.[1], g => g.[2]());
Drag options to blanks, or click blank then click option'
AKey
BCount
CLength
DSum
Attempts:
3 left
💡 Hint
Common Mistakes
Using Length is invalid for groups; Count() is the correct method.
Sum is for adding values, not counting elements.
5fill in blank
hard

Fill all three blanks to group numbers by even/odd and select the key and sum of each group.

C Sharp (C#)
var result = numbers.[1](n => n % 2 == 0).Select(g => new {
    [2] = g.Key,
    [3] = g.Sum()
});
Drag options to blanks, or click blank then click option'
AGroupBy
BKey
CSum
DCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using Count instead of Sum will count elements, not sum them.
Using Select instead of GroupBy will not group elements.