{TOC}
| Namespace: | System.Linq |
| Assembly: | System.Core.dll |
| Extends: | IEnumerable<T> |
Back to
Standard Query Operator IndexEditIntroduction
The SelectMany operator performs a one to many element projection over a sequence.
EditMethod Signatures
// 1 - Project the sub-elements of the source sequence in a new form.
public static IEnumerable<TResult> SelectMany<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, IEnumerable<TResult>> selector)
// 2 - Project sub-elements of the source into a new form.
// Also provides index position as an argument for use in the selector function.
public static IEnumerable<TResult> SelectMany<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, int, IEnumerable<TResult>> selector)
// New in January CTP
// 3 - Project sub-elements into a new form.
// Allows a select projection function for the sub-elements as well.
public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
this IEnumerable<TSource> source,
Func<TSource, IEnumerable<TCollection>> collectionSelector,
Func<TSource, TCollection, TResult> resultSelector)EditExceptions
Throws an ArgumentNullException if
source or
selector or
collectionSelector or
resultSelector are null.
EditPseudo-code
Overload 1
If
source is null, throw an ArgumentNullException.
If
selector is null, throw an ArgumentNullException.
Iterate the
source sequence.
Get the result of selector(current element) as sub-sequence.
Iterate the sub-sequence.
Return the current element in sub-sequence. Resume execution from this point when the next element is requested.
Overload 2 - source selector.
If
source is null, throw an ArgumentNullException.
If
selector is null, throw an ArgumentNullException.
Initialize a counter value to -1.
Iterate the
source sequence.
Get the result of selector(current element, counter) as sub-sequence.
Iterate the sub-sequence.
Return the current element in sub-sequence. Resume execution from this point when the next element is requested.
Note: The index value that is passed in as an argument is zero-based. That means the first element will have an index of 0, the second will have an index of 1, etc.
Overload 3 - source selector and sub-item result selector.
If
source is null, throw an ArgumentNullException.
If
collectionSelector is null, throw an ArgumentNullException.
If
resultSelector is null, throw an ArgumentNullException.
Iterate the
source sequence.
Get the result of collectionSelector(current element, counter) as sub-sequence.
Iterate the sub-sequence.
Return the result of the function resultSelector(current element in sub-sequence). Resume execution from this point when the next element is requested.
EditLoop Count
This operator implements the standard
deferred execution iterator pattern. This means, no looping will occur until the result is iterated over.
EditCode Samples
public static void SelectManyExample()
{
string[] sentence = new string[] {
"The quick brown", "fox jumped over","the lazy dog."};
// Select returns a three string[]
IEnumerable<string[]> words1 =
sentence.Select(w => w.Split(' '));
// To get each word, we have to use two foreach loops
foreach (string[] segment in words1)
foreach (string word in segment)
Console.WriteLine(word);
// SelectMany returns nine strings (sub-iterates the Select result)
IEnumerable<string> words2 =
sentence.SelectMany(segment => segment.Split(' '));
// With SelectMany we have every string individually
foreach (var word in words2)
Console.WriteLine(word);
IEnumerable<string> words3 =
from segment in sentence
from word in segment.Split(' ')
select word;
// With SelectMany we have every string individually
foreach (var w in words3)
Console.WriteLine(w);
Console.ReadLine();
}The
quick
brown
fox
jumped
over
the
lazy
dog.
The
quick
brown
fox
jumped
over
the
lazy
dog.
The
quick
brown
fox
jumped
over
the
lazy
dog.