Mega Code Archive
Returns true if there are enough items in the IEnumerableT
using System.Linq;
using System.Diagnostics;
namespace System.Collections.Generic
{
public static class EnumeratorExtensions
{
///
/// Returns true if there are enough items in the IEnumerable<T>.
///
///
/// The source list of items.
/// The number of items to check for.
/// True if there are enough items, false otherwise.
public static bool AtLeast(this IEnumerable source, int number)
{
if (source == null)
throw new ArgumentNullException("source");
int count = 0;
using (IEnumerator data = source.GetEnumerator())
while (count < number && data.MoveNext())
{
count++;
}
return count == number;
}
///
/// Returns true if there are enough items in the IEnumerable<T> to satisfy the condition.
///
///
/// The source list of items.
/// The number of items to check for.
/// The condition to apply to the items.
/// True if there are enough items, false otherwise.
public static bool AtLeast(this IEnumerable source, int number, Func condition)
{
if (source == null)
throw new ArgumentNullException("source");
if (condition == null)
throw new ArgumentNullException("condition");
int count = 0;
using (IEnumerator data = source.GetEnumerator())
while (count < number && data.MoveNext())
{
if (condition(data.Current))
count++;
}
return count == number;
}
}
}