ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

TakeUntil

The opposite of TakeWhile, inverts the expression and passes to TakeWhile so that instead of taking while an expression is true, you take until an expression is true.

Source

using System;
using System.Collections.Generic;
using System.Linq;


public static class EnumerableExtensions
{
	/// <summary>
	/// Continues processing items in a collection until the end condition is true.
	/// </summary>
	/// <typeparam name="T">The type of the collection.</typeparam>
	/// <param name="collection">The collection to iterate.</param>
	/// <param name="endCondition">The condition that returns true if iteration should stop.</param>
	/// <returns>Iterator of sub-list.</returns>
	public static IEnumerable<T> TakeUntil<T>(this IEnumerable<T> collection, Predicate<T> endCondition)
	{
		return collection.TakeWhile(item => !endCondition(item));
	}
}

Example

foreach(var item in list.TakeUntil(i => i.Name == null);

Author: James Michael Hare (BlackRabbitCoder)

Submitted on: 14 okt 2010

Language: C#

Type: System.Collections.Generic.IEnumerable<T>

Views: 8778