System.Linq
Provides classes and interfaces that support objects that can be enumerated, which have standard query operator implementations. This namespace is the foundation of LINQ.
Summary
The System.Linq namespace contains the core types and methods for Language Integrated Query (LINQ). LINQ enables you to write queries directly in C# or Visual Basic against a variety of data sources, including in-memory collections, databases, XML documents, and more.
Key features include:
- Query Operators: A rich set of methods (like
Where
,Select
,OrderBy
,GroupBy
) that perform common data manipulation tasks. - Deferred Execution: Queries are not executed until the results are actually needed, which can improve performance.
- Expression Trees: LINQ to SQL and other LINQ providers translate queries into expression trees that can be compiled and executed against external data sources.
Classes
-
Enumerable
Contains the static extension methods for querying collections that implement
IEnumerable<T>
. -
Queryable
Contains the static extension methods for querying objects that implement
IQueryable<T>
. Used primarily by LINQ providers.
Interfaces
-
IGrouping<TKey, TElement>
Represents a group of elements that have the same key.
-
ILookup<TKey, TElement>
Represents a collection of elements that are indexed by a specified key.
Extension Methods (Enumerable)
The System.Linq.Enumerable
class provides a wide range of extension methods for querying sequences.
-
Where<TSource>(IEnumerable<TSource>, Func<TSource, bool>)
Filters a sequence of values based on a predicate.
var evenNumbers = numbers.Where(n => n % 2 == 0);
-
Select<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>)
Projects each element of a sequence into a new form.
var names = users.Select(u => u.Name);
-
OrderBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)
Sorts the elements of a sequence in ascending order.
var sortedProducts = products.OrderBy(p => p.Price);
-
GroupBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)
Groups the elements of a sequence based on a specified key.
var groupedByCity = people.GroupBy(p => p.City);