MoreEnumerableTakeUntilTSource Method |
Returns items from the input sequence until the given predicate returns true
when applied to the current source item; that item will be the last returned.
Namespace:
MoreLinq
Assembly:
MoreLinq (in MoreLinq.dll) Version: 2.3.0
Syntax public static IEnumerable<TSource> TakeUntil<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate
)
<ExtensionAttribute>
Public Shared Function TakeUntil(Of TSource) (
source As IEnumerable(Of TSource),
predicate As Func(Of TSource, Boolean)
) As IEnumerable(Of TSource)
public:
[ExtensionAttribute]
generic<typename TSource>
static IEnumerable<TSource>^ TakeUntil(
IEnumerable<TSource>^ source,
Func<TSource, bool>^ predicate
)
[<ExtensionAttribute>]
static member TakeUntil :
source : IEnumerable<'TSource> *
predicate : Func<'TSource, bool> -> IEnumerable<'TSource>
Parameters
- source
- Type: System.Collections.GenericIEnumerableTSource
Source sequence - predicate
- Type: SystemFuncTSource, Boolean
Predicate used to determine when to stop yielding results from the source.
Type Parameters
- TSource
- Type of the source sequence
Return Value
Type:
IEnumerableTSourceItems from the source sequence, until the predicate returns true when applied to the item.
Usage Note
In Visual Basic and C#, you can call this method as an instance method on any object of type
IEnumerableTSource. When you use instance method syntax to call this method, omit the first parameter. For more information, see
Extension Methods (Visual Basic) or
Extension Methods (C# Programming Guide).
Exceptions Remarks
TakeUntil differs from Enumerable.TakeWhile in two respects. Firstly, the sense
of the predicate is reversed: it is expected that the predicate will return false
to start with, and then return true - for example, when trying to find a matching
item in a sequence.
Secondly, TakeUntil yields the element which causes the predicate to return true. For
example, in a sequence
and with a predicate of
, the result would be
.
TakeUntil is as lazy as possible: it will not iterate over the source sequence
until it has to, it won't iterate further than it has to, and it won't evaluate
the predicate until it has to. (This means that an item may be returned which would
actually cause the predicate to throw an exception if it were evaluated, so long as
no more items of data are requested.)
See Also