I wrote a special LINQ extension method that extends the method TakeWhile()so that it is included, and not exclusive, when the predicate is false.
public static IEnumerable<T> TakeWhile<T>(this IEnumerable<T> source, Func<T, bool> predicate, bool inclusive)
{
source.ThrowIfNull("source");
predicate.ThrowIfNull("predicate");
if (!inclusive)
return source.TakeWhile(predicate);
var totalCount = source.Count();
var count = source.TakeWhile(predicate).Count();
if (count == totalCount)
return source;
else
return source.Take(count + 1);
}
While this works, I am sure there is a better way to do this. I am sure this does not work in terms of deferred execution / loading.
ThrowIfNull()is an extension method for ArgumentNullExceptionchecking
Can the community provide some clues or rewrite? :)
source
share