Recall & Review
beginner
What does the
GroupBy method do in C#?It groups elements of a collection based on a key you specify, creating groups of items that share the same key.
Click to reveal answer
beginner
How do you access the key of each group in a
GroupBy result?Each group has a
Key property that holds the value used to group the elements.Click to reveal answer
intermediate
What type does
GroupBy return in C#?It returns an
IEnumerable<IGrouping<TKey, TElement>>, where each IGrouping represents a group of elements sharing the same key.Click to reveal answer
intermediate
Can you use
GroupBy to group by multiple properties?Yes, by creating an anonymous type as the key, like
GroupBy(x => new { x.Prop1, x.Prop2 }).Click to reveal answer
beginner
What is a simple example of using
GroupBy to group a list of words by their first letter?Example:<br>
var groups = words.GroupBy(w => w[0]);<br>foreach (var group in groups) {<br> Console.WriteLine($"Words starting with {group.Key}:");<br> foreach (var word in group) Console.WriteLine(word);<br>}Click to reveal answer
What does the
GroupBy method return?✗ Incorrect
GroupBy returns an IEnumerable<IGrouping<TKey, TElement>>, which is a collection of groups, each with a key and the elements in that group.
How do you specify the grouping key in
GroupBy?✗ Incorrect
You provide a function (lambda) that tells GroupBy how to get the key from each element.
Which property gives you the key of a group in
GroupBy?✗ Incorrect
Each group has a Key property representing the grouping key.
Can
GroupBy group by multiple properties?✗ Incorrect
You can group by multiple properties by creating an anonymous type as the key, like GroupBy(x => new { x.Prop1, x.Prop2 }).
What is the type of each element inside a group returned by
GroupBy?✗ Incorrect
Each group contains elements of the same type as the original collection.
Explain how the
GroupBy method works and how you can use it to group items in a list.Think about how you would sort items into labeled boxes based on a shared feature.
You got /4 concepts.
Describe how to iterate over groups created by
GroupBy and access both the group key and the items inside.Imagine opening each labeled box and looking at the items inside.
You got /3 concepts.