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#.

1. 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.
Example:
// 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);  

2. Expression Trees

When using LINQ with databases, C# builds expression trees instead of executing code directly. Example:
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  

3. 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).

4. 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.
  • Readable code with SQL-like queries.
  • Compile-time type checking (unlike SQL strings).
Would you like a full C# example combining these concepts?

Build docs developers (and LLMs) love