LINQ Basics in C# (Where, Select, OrderBy)
Learn LINQ basics in C# using Where, Select, and OrderBy to query, filter, and sort collections with practical examples.
In C#, LINQ (Language Integrated Query) is a powerful feature that makes it easy to query collections and data sources.
With LINQ, you can write SQL-like expressions on arrays, lists, XML, or databases.
In this article, we will cover the basic LINQ operators: Where, Select, and OrderBy.
Where
The Where method is used to filter elements that satisfy a certain condition.
It takes a predicate (a function that returns bool).
using System;
using System.Linq;
using System.Collections.Generic;
var numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Select even numbers
var evens = numbers.Where(n => n % 2 == 0);
Console.WriteLine(string.Join(", ", evens));
// Output: 2, 4, 6, 8, 10
Deferred Execution: When you write
var result = books.Where(...), the query does not execute immediately.
LINQ only creates a query definition. The filtering happens when the result
is enumerated (for example with foreach) or when methods like
ToList(), ToArray(), or Count() are called.
Deferred Execution Example
var numbers = new List<int>
{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};
var evens = numbers.Where(n => n % 2 == 0);
// Modify the collection after defining the query
numbers.Add(11);
numbers.Add(12);
numbers.Add(13);
numbers.Add(14);
// The query executes here
Console.WriteLine(string.Join(", ", evens));
// Output: 2, 4, 6, 8, 10, 12, 14
Notice that 12 and 14 are included in the result,
even though they were added after the Where statement.
This happens because Where uses deferred execution and evaluates
the collection at the time of enumeration.
Select
The Select operator is used to transform (project) each element of a collection.
For example, you can generate squares of numbers or select only certain properties from objects.
// Squares of numbers
var squares = numbers.Select(n => n * n);
Console.WriteLine(string.Join(", ", squares));
// Output: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
// Object example
var students = new List<(int Id, string Name)>
{
(1, "John"),
(2, "Mary"),
(3, "Michael")
};
// Select only names
var names = students.Select(s => s.Name);
Console.WriteLine(string.Join(", ", names));
// Output: John, Mary, Michael
OrderBy
The OrderBy method sorts elements according to a specified key.
By default, it sorts in ascending order. To sort in descending order, use OrderByDescending.
var fruits = new List<string> { "Apple", "Pear", "Banana", "Kiwi" };
// Sort alphabetically
var sorted = fruits.OrderBy(f => f);
Console.WriteLine(string.Join(", ", sorted));
// Output: Apple, Banana, Kiwi, Pear
// Sort by length in descending order
var byLength = fruits.OrderByDescending(f => f.Length);
Console.WriteLine(string.Join(", ", byLength));
// Output: Banana, Apple, Pear, Kiwi
Using Where + Select + OrderBy Together
LINQ operators are often chained together. In the following example: first filtering is applied, then transformation, and finally sorting.
var numbers2 = new List<int> { 10, 3, 7, 8, 2, 15, 6 };
// Get squares of numbers greater than 5, sorted ascending
var result = numbers2
.Where(n => n > 5)
.Select(n => n * n)
.OrderBy(n => n);
Console.WriteLine(string.Join(", ", result));
// Output: 36, 49, 64, 100, 225
Sample Application: Book List
In this scenario, we query a book list with LINQ:
filtering books with more than 200 pages (Where),
selecting only their titles (Select),
and ordering them by title length (OrderBy).
public class Book
{
public string Title { get; set; } = string.Empty;
public int Pages { get; set; }
}
class Program
{
static void Main()
{
var books = new List<Book>
{
new Book { Title = "C# Programming", Pages = 350 },
new Book { Title = "LINQ Guide", Pages = 220 },
new Book { Title = "Short Stories", Pages = 120 },
new Book { Title = "Algorithms", Pages = 400 }
};
var result = books
.Where(b => b.Pages > 200)
.Select(b => b.Title)
.OrderBy(t => t.Length);
Console.WriteLine("Selected Books:");
foreach (var title in result)
Console.WriteLine(title);
}
}
// Output:
// Selected Books:
// LINQ Guide
// C# Programming
// Algorithms
TL;DR
Where: Filters elements.Select: Transforms (projects) elements.OrderBy: Sorts elements (ascending/descending).- Operators can be chained:
Where → Select → OrderBy. - LINQ makes it easy to write SQL-like queries on collections.