CodeBlanks Logo Icon CodeBlanks

Summary - LINQ query syntax

C# course · Advanced · Lesson 36/83

Combine from, where, select and group by to write an employee report.

Summary - LINQ query syntax

In this group you learned the from, where, select, orderby and group by ... into clauses. A query starts with from, ends with select or group; in between you filter, sort and transform.

Now you will combine them to build an employee report: first filter high-paid employees with where, then sort by department and salary with orderby and produce a tailored output.

Exercise Filter high-paid employees

Filter employees with salary 5500 or higher in the employees list using from e in employees where e.Salary >= 5500 select e. Iterate with foreach and print each name with Console.WriteLine(e.Name).

Show example solution
Write var wellPaid = from e in employees where e.Salary >= 5500 select e;. Then loop with foreach (var e in wellPaid) { Console.WriteLine(e.Name); }.
Example solution:
using System;
using System.Collections.Generic;
using System.Linq;

class Employee
{
    public string Name { get; set; }
    public string Department { get; set; }
    public decimal Salary { get; set; }
}

class Program
{
    static void Main()
    {
        var employees = new List<Employee>
        {
            new Employee { Name = "Ayşe", Department = "Sales", Salary = 5000 },
            new Employee { Name = "Mehmet", Department = "IT", Salary = 7000 },
            new Employee { Name = "Zeynep", Department = "Sales", Salary = 5500 },
            new Employee { Name = "Alex", Department = "IT", Salary = 6500 }
        };
        var wellPaid = from e in employees
                      where e.Salary >= 5500
                      select e;
        foreach (var e in wellPaid)
        {
            Console.WriteLine(e.Name);
        }
    }
}

Exercise Sort and print the employee report

Sort the high-paid employees with orderby e.Department, e.Salary descending (ascending by department, descending by salary). Iterate with foreach and for each employee write Console.WriteLine($"{e.Department} - {e.Name}: {e.Salary}").

Show example solution
{e.Salary}"); }.
Example solution:
using System;
using System.Collections.Generic;
using System.Linq;

class Employee
{
    public string Name { get; set; }
    public string Department { get; set; }
    public decimal Salary { get; set; }
}

class Program
{
    static void Main()
    {
        var employees = new List<Employee>
        {
            new Employee { Name = "Ayşe", Department = "Sales", Salary = 5000 },
            new Employee { Name = "Mehmet", Department = "IT", Salary = 7000 },
            new Employee { Name = "Zeynep", Department = "Sales", Salary = 5500 },
            new Employee { Name = "Alex", Department = "IT", Salary = 6500 }
        };
        var report = from e in employees
                     where e.Salary >= 5500
                     orderby e.Department, e.Salary descending
                     select e;
        foreach (var e in report)
        {
            Console.WriteLine($"{e.Department} - {e.Name}: {e.Salary}");
        }
    }
}

Test yourself

  1. Which clause sorts in a query?

    • from
    • orderby
    • select
    • into
    Show answer

    orderby — Correct. Sorting is done with orderby.

  2. Match each clause to its position

    from
    Start
    where
    Filter
    orderby
    Sort
    select
    Result

All lessons in Advanced