CodeBlanks Logo Icon CodeBlanks

Summary - LINQ basics

C# course · Intermediate · Lesson 93/116

Combine Where, Select and OrderBy to write a student report.

Summary - LINQ basics

In this group you learned the core LINQ methods: Where filters, Select transforms, OrderBy sorts, First gets the first element. You can chain them into a single pipeline.

In the final lesson you will write a student report: filter students, sort them, and transform their names. This is a typical data-preparation flow in real applications: raw data goes in, a polished report comes out.

Exercise Write a student report

From the students list filter those with grade over 50 using Where, sort ascending by grade with OrderBy, take only the names with Select, and print each in a foreach loop.

Show example solution
Build the chain students.Where(s => s.Grade > 50).OrderBy(s => s.Grade).Select(s => s.Name), loop with foreach and print with Console.WriteLine(name).
Example solution:
using System;
using System.Collections.Generic;
using System.Linq;

class Student
{
    public string Name { get; set; }
    public int Grade { get; set; }
}

class Program
{
    static void Main()
    {
        var students = new List<Student>
        {
            new Student { Name = "Ayşe", Grade = 45 },
            new Student { Name = "Mehmet", Grade = 78 },
            new Student { Name = "Zeynep", Grade = 62 }
        };
        foreach (var name in students.Where(s => s.Grade > 50).OrderBy(s => s.Grade).Select(s => s.Name))
        {
            Console.WriteLine(name);
        }
    }
}

Exercise Top student

In the students list below filter those with grade over 50, sort descending with OrderByDescending and take the top one with First; print the name with Console.WriteLine.

Show example solution
Build the chain students.Where(s => s.Grade > 50).OrderByDescending(s => s.Grade).First(), read .Name and print.
Example solution:
using System;
using System.Collections.Generic;
using System.Linq;

class Student
{
    public string Name { get; set; }
    public int Grade { get; set; }
}

class Program
{
    static void Main()
    {
        var students = new List<Student>
        {
            new Student { Name = "Ayşe", Grade = 45 },
            new Student { Name = "Mehmet", Grade = 78 },
            new Student { Name = "Zeynep", Grade = 62 }
        };
        var top = students.Where(s => s.Grade > 50).OrderByDescending(s => s.Grade).First();
        Console.WriteLine(top.Name);
    }
}

Test yourself

  1. Which order is correct?

    • Select → Where → OrderBy
    • OrderBy → First → Select
    • Where → OrderBy → Select
    • First → Where → OrderBy
    Show answer

    Where → OrderBy → Select — Correct. First filter (Where), then sort (OrderBy), then transform (Select). This order is efficient and readable.

  2. Match each method to its purpose

    Where
    Filtering
    Select
    Transforming
    OrderBy
    Sorting
    First
    First element

All lessons in Intermediate