LINQ Extension Methods
A comprehensive guide to C# LINQ extension methods - Aggregate, All, Any, AsEnumerable, AsParallel, and more
LINQ Extension Methods
Using LINQ makes code more readable and organized. Here’s a collection of useful extension methods:
- Aggregate
- All
- Any
- AsEnumerable
- AsParallel
- Average
- Cast
- Concat
- Contains
- Count
- DefaultIfEmpty
- Distinct
- ElementAt
- ElementAtOrDefault
- Except
- First
- FirstOrDefault
- GroupBy
- GroupJoin
- Intersect
- Join
- Last
- LastOrDefault
- Max
- Min
- OfType
- OrderBy
- OrderByDescending
- Reverse
- Select
- SelectMany
- SequenceEqual
- Single
- SingleOrDefault
- Skip
- SkipWhile
- Sum
- Take
- TakeWhile
- ToArray
- ToDictionary
- ToList
- ToLookup
- Union
- Where
- Zip
Aggregate
Applies a method to each element. aggregating the sequence into a single value.
// 1. Basic form
public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource> func)
int[] sumArray = { 1, 2, 3, 4, 5 };
int result = sumArray.Aggregate((sum, next) => sum + next);
// sum: 0, next: 1 => 1
// sum: 1, next: 2 => 3
// sum: 3, next: 3 => 6
// sum: 6, next: 4 => 10
// sum: 10, next: 5 => 15
// 2. With seed value
public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func)
int[] sumArray = { 1, 2, 3, 4, 5 };
int result = sumArray.Aggregate(5, (sum, next) => sum + next);
// sum: 5, next: 1 => 6
// sum: 6, next: 2 => 8
// sum: 8, next: 3 => 11
// sum: 11, next: 4 => 15
// sum: 15, next: 5 => 20
// 3. With result selector
public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector)
int[] sumArray = { 1, 2, 3, 4, 5 };
int result = sumArray.Aggregate(3, (sum, next) => next > 2 ? sum + next : sum, result => result);
// sum: 3, next: 1 => 3
// sum: 3, next: 2 => 3
// sum: 3, next: 3 => 6
// sum: 6, next: 4 => 10
// sum: 10, next: 5 => 15
// result = 10 % 2
All
Checks if all elements in a collection match a specific condition.
public static bool All<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
int[] sumArray = { 1, 2, 3, 4, 5 };
bool isBiggerThanZero = sumArray.All(element => element > 0);
// true
bool isBiggerThanZeroOnEvens = (from number in sumArray
where number%2 == 0
select number).All(element => element > 0);
// IEnumerable returned with { 2, 4 }
Any
Checks if at least one element in a collection matches a specific condition.
public static bool Any<TSource>(this IEnumerable<TSource> source)
int[] sumArray = { 1, 2, 3, 4, 5 };
bool isBiggerThanFive = sumArray.Any(element => element > 5);
// false
bool isBiggerThanFour = sumArray.Any(element => element > 4);
// true
AsEnumerable
Casts a type implementing IEnumerable to a more specific type for easier LINQ operations.
public static IEnumerable<TSource> AsEnumerable<TSource>(this IEnumerable<TSource> source)
int[] sumArray = {1, 2, 3, 4, 5};
var query = sumArray.AsEnumerable();
foreach (var element in query)
{
// ...
}
AsParallel
Enables parallel processing for better performance.
int[] sumArray = {1, 2, 3, 4, 5};
int sum = sumArray.AsParallel().Sum();