Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »

SelectMany Operator

Modified: 2007/01/20 02:07 by t_magennis - Categorized as: LINQ to Objects
{TOC}
Namespace:System.Linq
Assembly:System.Core.dll
Extends:IEnumerable<T>

Back to Standard Query Operator Index


Edit

Introduction

The SelectMany operator performs a one to many element projection over a sequence.

Edit

Method 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)



Edit

Exceptions

Throws an ArgumentNullException if source or selector or collectionSelector or resultSelector are null.


Edit

Pseudo-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.

Edit

Loop Count

This operator implements the standard deferred execution iterator pattern. This means, no looping will occur until the result is iterated over.

Edit

Code 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.

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or ommissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain un-intentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.