Hooked on LINQ

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

Quick Search

Advanced Search »

TakeWhile Operator

Modified: 2007/01/24 02:24 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 TakeWhile operator yields elements from a sequence while a test is true and then skips the remainder of the sequence.

Edit

Method Signatures

// 1 - Return elements until the predicate returns false.
public static IEnumerable<TSource> TakeWhile<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, bool> predicate)
 
// 2 - Return elements until the predicate returns false.
// The predicate has access to the current elements index.
public static IEnumerable<TSource> TakeWhile<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, int, bool> predicate)
 



Edit

Exceptions

Throws an ArgumentNullException if source or predicate are null.

Edit

Pseudo-code

Overload 1
If source is null, throw an ArgumentNullException.
If predicate is null, throw an ArgumentNullException.
Iterate the source sequence.
If predicate(current element) returns false, then exit.
Return the current element. Resume execution from this point when the next element is requested.

Overload 2
If source is null, throw an ArgumentNullException.
If predicate is null, throw an ArgumentNullException.
Initialize a counter to zero.
Iterate the source sequence.
If predicate(current element, counter) returns false, then exit.
Return the current element. Resume execution from this point when the next element is requested.
Increment the counter by one.

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

TODO: Needs code sample.

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.