System.Linq Namespace

Provides classes and interfaces that support Language Integrated Query (LINQ), which adds native data querying capabilities to .NET languages.

Summary

The System.Linq namespace is the core of LINQ, offering the fundamental types and operations for querying collections, XML, databases, and other data sources. It introduces extension methods to common collection types, enabling a declarative and expressive way to filter, sort, group, and project data.

Key Concepts

Classes

Interfaces

Example Usage (LINQ to Objects)

Filtering and Projecting

Find all numbers greater than 5 and select their squares.

// C# Example
var numbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var results = numbers
.Where(n => n > 5)
.Select(n => n * n);

foreach (var square in results)
{
Console.WriteLine(square); // Output: 36, 49, 64, 81, 100
}

Grouping

Group words by their length.

// C# Example
var words = new[] { "apple", "banana", "kiwi", "orange", "grape", "pear" };

var groupedByLength = words
.GroupBy(w => w.Length);

foreach (var group in groupedByLength)
{
Console.WriteLine($"Words of length {group.Key}:");
foreach (var word in group)
{
Console.WriteLine($" - {word}");
}
}