1/23
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
GroupBy()
A LINQ grouping operator provided by Enumerable that organizes sequence elements into groups according to a selected key. Domain: C# → .NET → System.Linq → LINQ Grouping
var groups = people.GroupBy(p => p.Department);
Groups people according to their Department value so people with the same department belong to the same group.
var groups = students.GroupBy(s => s.Grade);
Groups students according to their Grade value.
var groups = words.GroupBy(word => word[0]);
Groups strings according to their first character.
var groups = numbers.GroupBy(n => n % 2 == 0 ? "Even" : "Odd");
Groups numbers into two groups whose keys are "Even" and "Odd".
Key Selector
A function supplied to GroupBy() that extracts the value used to determine which group each source element belongs to.
IEnumerable
Stores grouping results where each IGrouping
IGrouping
An interface representing one LINQ group consisting of a Key and a sequence of elements associated with that key.
var key = group.Key;
Gets the key that identifies a particular IGrouping produced by GroupBy().
foreach (var group in people.GroupBy(p => p.Department))
Enumerates each group produced by GroupBy(), allowing each group and its elements to be processed separately.
foreach (var person in group)
Enumerates the individual elements contained inside a particular LINQ group.
var groups = people.GroupBy(p => p.Department, p => p.Name);
Groups people by Department but stores only each person's Name as the elements inside the resulting groups.
Element Selector
A function used by a GroupBy() overload to determine what value from each source element should actually be stored inside its group.
var results = people.GroupBy(p => p.Department).Select(g => new { Department = g.Key, Count = g.Count() });
Groups people by Department and projects each resulting group into an object containing the department key and the number of people in that group.
var results = students.GroupBy(s => s.Course).Select(g => new { Course = g.Key, Average = g.Average(s => s.Grade) });
Groups students by Course and calculates the average Grade within each course group.
var groups = people.GroupBy(p => new { p.Country, p.City });
Groups elements using a composite key containing multiple values, so elements belong together only when both Country and City match.
Composite Grouping Key
A grouping key composed of multiple values, allowing elements to be grouped according to a combination of properties rather than only one property.
ToLookup()
A LINQ conversion operation that immediately creates an ILookup
var lookup = people.ToLookup(p => p.Department);
Creates a lookup in which each Department key maps to the people belonging to that department.
ILookup
An interface representing a one-to-many mapping from keys to sequences of associated elements, similar to a dictionary where each key can correspond to multiple values.
var engineering = lookup["Engineering"];
Retrieves the sequence of elements associated with the "Engineering" key from an ILookup.
bool exists = lookup.Contains("Engineering");
Checks whether an ILookup contains the specified key.
GroupBy() vs ToLookup()
GroupBy() normally uses deferred execution and produces groupings when enumerated, whereas ToLookup() executes immediately and creates a reusable key-to-elements lookup structure.
Grouping
The operation of partitioning sequence elements into collections whose members share the same selected key.