Skip to main content
LINQ is Language Integrated Query. It allows you to query collections (like arrays, lists, databases, XML) using a SQL-like syntax directly in C#.

LINQ Providers

LINQ works through providers that translate your queries into different commands:
  • LINQ to Objects: For in-memory collections (List, arrays)
  • LINQ to SQL: Translates to SQL commands for databases
  • LINQ to XML: For querying XML documents
// LINQ to Objects
var numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = from num in numbers
                  where num % 2 == 0
                  select num;
// Or with method syntax:
var evenNumbers2 = numbers.Where(num => num % 2 == 0);

Expression Trees

When using LINQ with databases, C# builds expression trees instead of executing code directly.
IQueryable<Product> query = dbContext.Products
    .Where(p => p.Price > 100);
Here, p => p.Price > 100 is converted into an expression tree, which LINQ to SQL translates to:
SELECT * FROM Products WHERE Price > 100

Custom LINQ Providers

You can create your own LINQ provider by implementing IQueryProvider and IQueryable. This is advanced but powerful for translating queries to custom data sources (e.g., a REST API).

Parallel LINQ (PLINQ)

For performance, use AsParallel() to execute queries across multiple threads:
var largeList = Enumerable.Range(1, 1000000);
var parallelQuery = largeList.AsParallel()
    .Where(x => x % 2 == 0)
    .ToList();

Key Benefits

Unified syntax for different data sources - Query in-memory collections, databases, and XML with the same approach.
Readable code with SQL-like queries makes complex data operations intuitive and maintainable.
Compile-time type checking (unlike SQL strings) catches errors early in development.

Build docs developers (and LLMs) love