Advertisement
Intermediate C# Lesson 1 of 8

Lesson 1: Introduction to LINQ in C#

Language Integrated Query (LINQ) brings query capabilities directly into C#. Instead of writing loops and manual filters, LINQ lets you express data operations declaratively with a fluent, readable syntax.

Advertisement

What is LINQ?

LINQ is a set of APIs that let you query in-memory collections, databases, XML documents, and more using a consistent syntax. It works with IEnumerable<T> and IQueryable<T> and translates queries to the underlying provider when needed.

LINQ Providers

Common LINQ providers include:

  • LINQ to Objects — query arrays, lists, and other in-memory collections.
  • LINQ to XML — query and transform XML documents.
  • LINQ to SQL / Entity Framework — query relational databases using C# expressions.

💡 Tip: LINQ is not a single language — it is integrated query support built into C# using extension methods and expression trees.

Query Syntax vs Method Syntax

LINQ supports two main styles: query syntax that looks like SQL, and method syntax that uses chained extension methods. Under the hood, both produce the same results for LINQ to Objects.

int[] numbers = { 1, 2, 3, 4, 5, 6 };

// Query syntax
var evens =
    from n in numbers
    where n % 2 == 0
    select n;

// Method syntax
var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();

Deferred Execution

LINQ queries are usually deferred. That means the query is not executed until you enumerate it with a loop or a terminal operator like ToList() or Count().

var query = numbers.Where(n => n > 2);

// Query not executed yet
numbers[0] = 99;

foreach (var n in query)
{
    Console.WriteLine(n); // prints values after the change
}

Why LINQ?

LINQ makes data code easier to read and maintain. It reduces boilerplate, centralizes filtering and projection logic, and works consistently across many data sources.

🧠 Quick Check — Lesson 1

Which statement is true about LINQ in C#?

Lesson Summary

LINQ unifies queries across objects, XML, and databases with a consistent C# syntax.

Query syntax resembles SQL, while method syntax uses chained extension methods like Where and Select.

LINQ queries are usually deferred until you enumerate results or call a terminal operator.

LINQ providers determine how the query is executed and translated to the underlying data source.

Up Next

Lesson 2: Method Syntax & Extension Methods

Next Lesson →