LINQ Grouping

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/23

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 3:29 AM on 8/8/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

24 Terms

1
New cards

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

2
New cards

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.

3
New cards

var groups = students.GroupBy(s => s.Grade);

Groups students according to their Grade value.

4
New cards

var groups = words.GroupBy(word => word[0]);

Groups strings according to their first character.

5
New cards

var groups = numbers.GroupBy(n => n % 2 == 0 ? "Even" : "Odd");

Groups numbers into two groups whose keys are "Even" and "Odd".

6
New cards

Key Selector

A function supplied to GroupBy() that extracts the value used to determine which group each source element belongs to.

7
New cards

IEnumerable

Stores grouping results where each IGrouping

8
New cards

IGrouping

An interface representing one LINQ group consisting of a Key and a sequence of elements associated with that key.

9
New cards

var key = group.Key;

Gets the key that identifies a particular IGrouping produced by GroupBy().

10
New cards

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.

11
New cards

foreach (var person in group)

Enumerates the individual elements contained inside a particular LINQ group.

12
New cards

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.

13
New cards

Element Selector

A function used by a GroupBy() overload to determine what value from each source element should actually be stored inside its group.

14
New cards

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.

15
New cards

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.

16
New cards

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.

17
New cards

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.

18
New cards

ToLookup()

A LINQ conversion operation that immediately creates an ILookup

19
New cards

var lookup = people.ToLookup(p => p.Department);

Creates a lookup in which each Department key maps to the people belonging to that department.

20
New cards

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.

21
New cards

var engineering = lookup["Engineering"];

Retrieves the sequence of elements associated with the "Engineering" key from an ILookup.

22
New cards

bool exists = lookup.Contains("Engineering");

Checks whether an ILookup contains the specified key.

23
New cards

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.

24
New cards

Grouping

The operation of partitioning sequence elements into collections whose members share the same selected key.