Class: Enumerable
Inheritance: object
Class Overview
The System.Linq.Enumerable class provides a set of static extension methods that operate on IEnumerable<T> objects. These methods are the core of Language Integrated Query (LINQ) and enable powerful data manipulation and querying capabilities directly within C# and Visual Basic code.
This class contains methods for filtering, projecting, ordering, grouping, and performing aggregate operations on sequences. It's fundamental for working with collections in a declarative and expressive way.
Members
-
All<TSource>(IEnumerable<TSource>, Func<TSource, bool>)
bool All<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)Tests whether all elements of a sequence satisfy a condition.
- Parameters:
-
source: System.Collections.Generic.IEnumerable<TSource>
AnIEnumerable<T>whose elements to test for all matching the condition. -
predicate: System.Func<TSource, bool>
A function to test each element for a condition.
- Returns:
trueif all elements in the sequence pass the test in the specified predicate, or if the sequence is empty; otherwise,false.
- Remarks:
- If
sourceis empty,Allreturnstrue.
- Example:
// Define a list of integers var numbers = new List{ 2, 4, 6, 8, 10 }; // Check if all numbers are even bool allEven = numbers.All(n => n % 2 == 0); // Output: True Console.WriteLine(allEven); -
Any<TSource>(IEnumerable<TSource>, Func<TSource, bool>)
bool Any<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)Tests whether any element of a sequence satisfies a condition.
- Parameters:
-
source: System.Collections.Generic.IEnumerable<TSource>
AnIEnumerable<T>whose elements to test for any matching the condition. -
predicate: System.Func<TSource, bool>
A function to test each element for a condition.
- Returns:
trueif any element of the sequence passes the test in the specified predicate; otherwise,false.
- Remarks:
- If
sourceis empty,Anyreturnsfalse.
- Example:
// Define a list of integers var numbers = new List{ 1, 3, 5, 7, 9 }; // Check if any number is even bool anyEven = numbers.Any(n => n % 2 == 0); // Output: False Console.WriteLine(anyEven); -
Count<TSource>(IEnumerable<TSource>)
int Count<TSource>(this IEnumerable<TSource> source)Returns the number of elements in a sequence.
- Parameters:
-
source: System.Collections.Generic.IEnumerable<TSource>
TheIEnumerable<T>that contains the elements to be counted.
- Returns:
- The number of elements in the sequence.
- Remarks:
- If
sourceimplementsICollection<T>, that implementation'sCountproperty is returned. Otherwise, this method iterates through the sequence and counts its elements.
- Example:
var names = new List<string> { "Alice", "Bob", "Charlie" }; int count = names.Count(); // Output: 3 Console.WriteLine(count); -
Where<TSource>(IEnumerable<TSource>, Func<TSource, bool>)
IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)Filters a sequence of values based on a predicate.
- Parameters:
-
source: System.Collections.Generic.IEnumerable<TSource>
AnIEnumerable<T>to filter. -
predicate: System.Func<TSource, bool>
A function to test each element for a condition.
- Returns:
- An
IEnumerable<T>that contains elements from the input sequence that satisfy the condition.
- Example:
var numbers = new List<int> { 1, 2, 3, 4, 5, 6 }; var evenNumbers = numbers.Where(n => n % 2 == 0); // Output: 2, 4, 6 foreach (var num in evenNumbers) { Console.Write($"{num} "); } Console.WriteLine(); -
Select<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>)
IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)Projects each element of a sequence into a new form.
- Parameters:
-
source: System.Collections.Generic.IEnumerable<TSource>
A sequence of values to invoke a transform function on. -
selector: System.Func<TSource, TResult>
A transform function to apply to each element.
- Returns:
- An
IEnumerable<TResult>whose elements are the result of invoking the transform function on each element of the given sequence.
- Example:
var words = new List<string> { "apple", "banana", "cherry" }; var lengths = words.Select(w => w.Length); // Output: 5, 6, 6 foreach (var len in lengths) { Console.Write($"{len} "); } Console.WriteLine(); -
OrderBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)
IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)Sorts the elements of a sequence in ascending order according to a key.
- Parameters:
-
source: System.Collections.Generic.IEnumerable<TSource>
The sequence of values to order. -
keySelector: System.Func<TSource, TKey>
A function to extract a key from each element.
- Returns:
- An
IOrderedEnumerable<TSource>whose elements are sorted in ascending order by key.
- Example:
var products = new List<Product> { new Product { Name = "Laptop", Price = 1200 }, new Product { Name = "Keyboard", Price = 75 }, new Product { Name = "Mouse", Price = 25 } }; var sortedProducts = products.OrderBy(p => p.Price); // Output: Mouse (25), Keyboard (75), Laptop (1200) foreach (var p in sortedProducts) { Console.WriteLine($"{p.Name} ({p.Price})"); } -
GroupBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)
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 selector.
- Parameters:
-
source: System.Collections.Generic.IEnumerable<TSource>
The sequence of values to group. -
keySelector: System.Func<TSource, TKey>
A function to extract the key from each element.
- Returns:
- An
IEnumerable<T>ofIGrouping<TKey, TSource>, where each group represents a unique key.
- Example:
var people = new List<Person> { new Person { Name = "Alice", City = "New York" }, new Person { Name = "Bob", City = "London" }, new Person { Name = "Charlie", City = "New York" }, new Person { Name = "David", City = "London" } }; var groupedByCity = people.GroupBy(p => p.City); foreach (var group in groupedByCity) { Console.WriteLine($"City: {group.Key}"); foreach (var person in group) { Console.WriteLine($"- {person.Name}"); } } // Output: // City: New York // - Alice // - Charlie // City: London // - Bob // - David