Group by in LINQ: 5 Questions That Bother Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Group by in LINQ: Understanding Grouping with Real Examples

\n\n

If you've ever written queries to collections in C#, you've likely faced the task: "How do I group data?". The topic of group by in LINQ is one of the most popular on Stack Overflow (tens of thousands of views). And this is no coincidence: grouping seems simple only at first glance. In reality, beginners stumble over typical pitfalls: how to select the first record in a group, how to calculate a sum, or how to do nested grouping.

\n\n

In this article, we'll cover the 5 most frequent questions about LINQ GroupBy and provide working answers. Let's go!

\n\n

1. How does GroupBy work in LINQ? Basic Syntax

\n\n

The GroupBy method is an analog of the SQL GROUP BY operator. It splits a sequence into groups based on a specified key. The result is a collection of IGrouping<TKey, TElement> objects, where each object contains the key and all elements that fall into that group.

\n\n

Here's a simple example. Let's say we have a list of students:

\n\n
public class Student\n{\n    public string Name { get; set; }\n    public string Group { get; set; }\n    public int Score { get; set; }\n}\n\nvar students = new List<Student>\n{\n    new Student { Name = "Anna", Group = "A", Score = 85 },\n    new Student { Name = "Boris", Group = "B", Score = 90 },\n    new Student { Name = "Vika", Group = "A", Score = 78 },\n    new Student { Name = "Gleb", Group = "B", Score = 92 }\n};
\n\n

Grouping by group name:

\n\n
var grouped = students.GroupBy(s => s.Group);\n\nforeach (var group in grouped)\n{\n    Console.WriteLine($"Group: {group.Key}");\n    foreach (var student in group)\n    {\n        Console.WriteLine($"  {student.Name} — {student.Score}");\n    }\n}
\n\n

Output:

\n
Group: A\n  Anna — 85\n  Vika — 78\nGroup: B\n  Boris — 90\n  Gleb — 92
\n\n

It's simple. But the devil, as always, is in the details.

\n\n

2. How to select the first record from each group?

\n\n

This is the most popular question about group by in LINQ. Let's say we need to get one best student from each group (with the highest score). How to do this?

\n\n

The solution is to combine GroupBy with First() or OrderByDescending:

\n\n
var bestStudents = students\n    .GroupBy(s => s.Group)\n    .Select(g => g.OrderByDescending(s => s.Score).First());\n\nforeach (var student in bestStudents)\n{\n    Console.WriteLine($"{student.Name} — {student.Group} — {student.Score}");\n}
\n\n

Result: Anna (85) and Gleb (92). Note: inside Select, we sort the group and take the first element. This is the answer to the question "how to do LINQ group by first".

\n\n

Important: First() will throw an exception if the group is empty. If that's possible, use FirstOrDefault().

\n\n

3. How to calculate the sum, count, or average per group?

\n\n

Often after grouping, you need not just to iterate over elements, but to get aggregated values. For example, the total score per group or the average score.

\n\n

Use Sum, Count, Average:

\n\n
var groupStats = students\n    .GroupBy(s => s.Group)\n    .Select(g => new\n    {\n        Group = g.Key,\n        Count = g.Count(),\n        TotalScore = g.Sum(s => s.Score),\n        AverageScore = g.Average(s => s.Score)\n    });\n\nforeach (var stat in groupStats)\n{\n    Console.WriteLine($"Group {stat.Group}: {stat.Count} students, total {stat.TotalScore}, average {stat.AverageScore:F2}");\n}
\n\n

Output:

\n
Group A: 2 students, total 163, average 81.50\nGroup B: 2 students, total 182, average 91.00
\n\n

You can also use Min and Max — in

Blogs

Book Recommendations