Home About Lessons Blog On The Mic Contact Projects
Lessons BASIC · C#

C# Basics — LINQ

10 min read · Emre Ulutabak
1
What is LINQ?

You have a list of 100 people. You want to find only those older than 30. You'd write a loop, add an if, push to a new list — many lines of code.

LINQ (Language Integrated Query) does this in one line. It is a powerful query language built directly into C#.

2
Where — filtering

Where takes a condition and returns only the elements that satisfy it. It works just like WHERE in SQL.

csharp
var sayilar = new List<int> { 5, 12, 3, 18, 7, 25 };

var buyukler = sayilar.Where(x => x > 10).ToList();
// Sonuç: [12, 18, 25]
3
Select — transforming

Select transforms each element. It is used to produce a new list from an existing one — like SELECT in SQL.

csharp
var isimler = new List<string> { "ali", "veli", "ayşe" };

var buyukHarf = isimler.Select(x => x.ToUpper()).ToList();
// Sonuç: ["ALI", "VELI", "AYŞE"]
4
OrderBy — sorting
csharp
var sayilar = new List<int> { 5, 12, 3, 18, 7 };

var sirali = sayilar.OrderBy(x => x).ToList();
// Sonuç: [3, 5, 7, 12, 18]

var tersSirali = sayilar.OrderByDescending(x => x).ToList();
// Sonuç: [18, 12, 7, 5, 3]
5
Chaining

The power of LINQ is in chaining. You can write multiple operations one after another — filter, transform, sort, all in one line.

csharp
var urunler = new List<string> { "elma", "armut", "erik", "kiraz", "kayısı" };

var sonuc = urunler
    .Where(x => x.Length > 4)       // 4 harften uzun olanlar
    .Select(x => x.ToUpper())        // büyük harfe çevir
    .OrderBy(x => x)                 // alfabetik sırala
    .ToList();

// Sonuç: ["ARMUT", "KAYISI", "KİRAZ"]
💡
Don't forget ToList() — LINQ queries use lazy evaluation and are only executed when you call ToList().
6
Golden rules
💡
LINQ improves readability. Prefer Where + Select over loops with if statements.
💡
FirstOrDefault(), Any(), Count(), Sum() are all part of LINQ too.
💡
Entity Framework uses the same LINQ syntax — what you learn here works on databases too.
MINI QUIZ
Which is correct about LINQ?
LINQ is only used for database queries
The Where method filters a collection
LINQ methods cannot be chained
The Select method sorts a collection