Aral Code

Going Further Lesson 13 of 15

C# LINQ — Querying Collections

Filter, sort, transform and summarize collections with LINQ — Where, Select, OrderBy, First, Any, Count, Sum, Average, GroupBy — using lambdas, with class record examples.

LINQ (Language Integrated Query) lets you ask questions about collections in a short, readable way. Instead of writing loops to filter and sort, you chain methods like Where, OrderBy and Select. If you know SQL, it will feel familiar.

Lambdas

LINQ methods take small functions called lambdas, written with =>. g => g >= 75 means "given g, return whether g is at least 75".

Filtering, sorting and transforming

int[] grades = { 88, 72, 95, 60, 81, 90 };

var passing = grades.Where(g => g >= 75);
var sorted = grades.OrderByDescending(g => g);
var curved = grades.Select(g => Math.Min(100, g + 5));

Console.WriteLine(string.Join(", ", passing));
Console.WriteLine(string.Join(", ", sorted));
Console.WriteLine(string.Join(", ", curved));

Output

88, 95, 81, 90
95, 90, 88, 81, 72, 60
93, 77, 100, 65, 86, 95
  • Where keeps items that match a condition.
  • OrderBy / OrderByDescending sort.
  • Select transforms each item.

Summaries

int[] grades = { 88, 72, 95, 60, 81, 90 };
Console.WriteLine($"Count: {grades.Count()}");
Console.WriteLine($"Passing: {grades.Count(g => g >= 75)}");
Console.WriteLine($"Sum: {grades.Sum()}");
Console.WriteLine($"Average: {grades.Average():F2}");
Console.WriteLine($"Max: {grades.Max()}, Min: {grades.Min()}");
Console.WriteLine($"Any failing? {grades.Any(g => g < 75)}");
Console.WriteLine($"All above 50? {grades.All(g => g > 50)}");
Console.WriteLine($"First above 90: {grades.First(g => g > 90)}");

Output

Count: 6
Passing: 4
Sum: 486
Average: 81.00
Max: 95, Min: 60
Any failing? True
All above 50? True
First above 90: 95

LINQ with objects

var students = new List<Student>
{
    new("Ana", "Rizal", 91.5),
    new("Ben", "Bonifacio", 84.0),
    new("Carla", "Rizal", 95.2),
    new("Dan", "Bonifacio", 72.4),
    new("Ela", "Rizal", 88.8)
};

var honors = students
    .Where(s => s.Average >= 90)
    .OrderByDescending(s => s.Average)
    .Select(s => $"{s.Name} ({s.Average})");
Console.WriteLine("With honors: " + string.Join(", ", honors));

var top = students.MaxBy(s => s.Average);
Console.WriteLine($"Top student: {top?.Name}");

record Student(string Name, string Section, double Average);

Output

With honors: Carla (95.2), Ana (91.5)
Top student: Carla

Chaining reads like a sentence: "students where average ≥ 90, ordered by average, selected as text".

GroupBy

var students = new List<Student>
{
    new("Ana", "Rizal", 91.5), new("Ben", "Bonifacio", 84.0),
    new("Carla", "Rizal", 95.2), new("Dan", "Bonifacio", 72.4),
    new("Ela", "Rizal", 88.8)
};

foreach (var group in students.GroupBy(s => s.Section).OrderBy(g => g.Key))
{
    Console.WriteLine($"{group.Key}: {group.Count()} students, average {group.Average(s => s.Average):F2}");
}

record Student(string Name, string Section, double Average);

Output

Bonifacio: 2 students, average 78.20
Rizal: 3 students, average 91.83

Query syntax

LINQ also has an SQL-like syntax. Both forms do the same thing:

int[] grades = { 88, 72, 95, 60, 81 };
var result = from g in grades
             where g >= 75
             orderby g descending
             select g;
Console.WriteLine(string.Join(", ", result));

Output

95, 88, 81

Most C# developers use the method syntax (Where(...)), but you'll see query syntax in older code and tutorials.

Deferred execution and ToList

A LINQ query doesn't run until you use the results (in a loop, Count(), ToList()…). Call .ToList() to run it once and store the results, especially if you'll use them several times.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which LINQ method keeps only items that match a condition?
2. What does Select(g => g + 5) do?
3. What happens with First(x => x > 100) if no item is above 100?
4. What does GroupBy(s => s.Section) produce?

By Sir Reyel · Last updated 2026-12-04