System.Linq Namespace

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

Summary

Enumerable class Enumerable
Provides a set of static methods for querying sequences. This is the core class for LINQ to Objects.
Members: 50+ Methods
Queryable class Queryable
Provides a set of static methods that return common Language-Integrated Query (LINQ) operations for any data source that implements IQueryable<T>.
Members: 50+ Methods
IGrouping<TKey, TElement> interface IGrouping<TKey, TElement>
Represents a group of elements that have the same key.
Members: 2 Interfaces
ILookup<TKey, TElement> interface ILookup<TKey, TElement>
Represents a collection of elements indexed by a key. This interface is implemented by Lookup<TKey, TElement>.
Members: 4 Interfaces

Key Concepts

LINQ enables you to write queries directly in your programming language. It offers:

Common Operations

Filtering

Where static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
Filters a sequence of values based on a predicate.

var numbers = new[] { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(n => n % 2 == 0);
// Result: [2, 4]
                    
FilteringPredicate

Projection

Select static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
Projects each element of a sequence into a new form.

var names = new[] { "Alice", "Bob", "Charlie" };
var nameLengths = names.Select(name => name.Length);
// Result: [5, 3, 7]
                    
ProjectionTransformation

Ordering

OrderBy static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
Sorts the elements of a sequence in ascending order.

var products = new[] {
    new { Name = "Apple", Price = 1.0 },
    new { Name = "Banana", Price = 0.5 },
    new { Name = "Orange", Price = 0.75 }
};
var sortedProducts = products.OrderBy(p => p.Price);
// Result: [{ Name = "Banana", Price = 0.5 }, { Name = "Orange", Price = 0.75 }, { Name = "Apple", Price = 1.0 }]
                    
SortingOrderingAscending

Grouping

GroupBy static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
Groups the elements of a sequence by a specified key.

var numbers = new[] { 1, 2, 3, 4, 5, 6 };
var evenOddGroups = numbers.GroupBy(n => n % 2 == 0 ? "Even" : "Odd");
// Result: Groups named "Even" and "Odd" containing their respective numbers.
                    
GroupingKey

Further Reading