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));
foreach(var item in list.TakeUntil(i => i.Name == null);


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.


Double click on the code to select all.