Windows System Documentation

LINQ (Language Integrated Query)

LINQ provides a consistent, powerful, and flexible way to query data from various sources directly within the C# and Visual Basic programming languages. It extends the expressive power of C# and Visual Basic with built-in query capabilities that are syntactically similar to SQL.

Overview

LINQ offers a uniform querying experience across different types of data. It can query data from objects in memory (like collections and arrays), XML documents, databases, and more. The core idea is to bring query capabilities into the language itself, enabling compile-time checking of queries and IntelliSense support.

Key Concepts

Core Components

1. Query Operators

LINQ provides a set of standard query operators that can be used to perform common query tasks. These operators are implemented as extension methods on IEnumerable<T> and IQueryable<T>.

Common Operators:

2. Query Syntax vs. Method Syntax

LINQ queries can be written in two syntaxes:

Query Syntax (Declarative)

This syntax is often more readable for complex queries and resembles SQL.

var querySyntax = from num in numbers
                              where num % 2 == 0
                              orderby num descending
                              select num;
Method Syntax (Fluent API)

This syntax uses extension methods and is often more concise. All LINQ queries can be expressed in method syntax.

var methodSyntax = numbers.Where(num => num % 2 == 0)
                                      .OrderByDescending(num => num)
                                      .Select(num => num);

3. Deferred Execution

Most LINQ query operators do not execute immediately. Instead, they store the query information. The actual query execution is deferred until you iterate over the query results (e.g., using a foreach loop) or call a method that requires immediate results (like Count(), ToList(), ToArray()).

Example: Deferred Execution

List<int> numbers = new List<int> { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var evenNumbersQuery = numbers.Where(n => n % 2 == 0); // Query is defined, not executed yet

// Outputting numbers before adding more
Console.WriteLine("Even numbers before modification:");
foreach (var n in evenNumbersQuery)
{
    Console.Write(n + " "); // Execution happens here
}
Console.WriteLine();

numbers.Add(10); // Add a new element

// Outputting numbers again - the query now includes 10
Console.WriteLine("Even numbers after modification:");
foreach (var n in evenNumbersQuery) // Execution happens again with updated data
{
    Console.Write(n + " ");
}
Console.WriteLine();

LINQ to Objects

This is the most common LINQ implementation, operating on in-memory collections like arrays, lists, and dictionaries. It leverages the System.Linq namespace and its extension methods.

LINQ to SQL / Entity Framework

These providers allow LINQ to query relational databases. The query syntax or method syntax is translated into SQL (or other database-specific query languages) and executed against the database. This enables powerful and type-safe database interactions.

Common Use Cases

Best Practices