Many LINQ functions both operate on an IEnumerable<TSource>
and also return an IEnumerable<TResult>
. The type parameters TSource
and TResult
may or may not refer to the same type, depending on the method in question and any functions passed to it.
A few examples of this are
public static IEnumerable<TResult> Select<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TResult> selector
)
public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, int, bool> predicate
)
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector
)
While some method chaining may require an entire set to be worked prior to moving on, LINQ takes advantage of deferred execution by using yield return MSDN which creates an Enumerable and an Enumerator behind the scenes. The process of chaining in LINQ is essentially building an enumerable (iterator) for the original set -- which is deferred -- until materialized by enumerating the enumerable.
This allows these functions to be fluently chained wiki, where one function can act directly on the result of another. This style of code can be used to perform many sequence based operations in a single statement.
For example, it's possible to combine Select
, Where
and OrderBy
to transform, filter and sort a sequence in a single statement.
var someNumbers = { 4, 3, 2, 1 };
var processed = someNumbers
.Select(n => n * 2) // Multiply each number by 2
.Where(n => n != 6) // Keep all the results, except for 6
.OrderBy(n => n); // Sort in ascending order
Output:
2
4
8
Any functions that both extend and return the generic IEnumerable<T>
type can be used as chained clauses in a single statement. This style of fluent programming is powerful, and should be considered when creating your own extension methods.