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

GroupBy operation in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AIEnumerable of groups with a key and elements
BA single grouped object
CA sorted list
DA dictionary with keys and values
How do you specify the grouping key in GroupBy?
ABy filtering elements
BBy sorting the collection first
CBy passing a function that returns the key for each element
DBy converting elements to strings
Which property gives you the key of a group in GroupBy?
AValue
BKey
CGroup
DName
Can GroupBy group by multiple properties?
AYes, but only with a tuple
BNo, only one property is allowed
CNo, you must call <code>GroupBy</code> multiple times
DYes, by using an anonymous type as the key
What is the type of each element inside a group returned by GroupBy?
AThe original element type from the source collection
BA string representing the key
CAn integer index
DA dictionary entry
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.