Digamos que quiero verificar si hay al menos N elementos en una colección.Recuento de LINQ() hasta que, ¿es esto más eficiente?
¿Es esto mejor que hacer?
Count() >= N
Usando:
public static bool AtLeast<T>(this IEnumerable<T> enumerable, int max)
{
int count = 0;
return enumerable.Any(item => ++count >= max);
}
O incluso
public static bool Equals<T>(this IEnumerable<T> enumerable, int amount)
{
return enumerable.Take(amount).Count() == amount;
}
¿Cómo podría este punto de referencia?
/// <summary>
/// Returns whether the enumerable has at least the provided amount of elements.
/// </summary>
public static bool HasAtLeast<T>(this IEnumerable<T> enumerable, int amount)
{
return enumerable.Take(amount).Count() == amount;
}
/// <summary>
/// Returns whether the enumerable has at most the provided amount of elements.
/// </summary>
public static bool HasAtMost<T>(this IEnumerable<T> enumerable, int amount)
{
return enumerable.Take(amount + 1).Count() <= amount;
}
_Cómo iba a hacerlo esta referencia _ - Ponga el código que normalmente se utiliza para invocarlos en un bucle con el tiempo ..? –
otra opción: enumerable.Select ((o, idx) => idx). Cualquier (i => i> = max); – KristoferA