Last active
November 6, 2025 16:24
-
-
Save chrisfcarroll/524ff4f7a5c76a5f9f10dbf888b5481f to your computer and use it in GitHub Desktop.
IEnumerable<T>?.None() , IEnumerable<T>?.None(predicate) , IEnumerable<T>?.HasCountAtLeast()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class EnumerablePredicateExtensions | |
| { | |
| /// <returns><c>true</c> if <paramref name="source"/>has no elements</returns> | |
| public static bool IsEmpty<T>(this IEnumerable<T> source) | |
| => !source.Any(); | |
| ///<summary>Synonym for <see cref="None"/></summary> | |
| /// <returns><c>true</c> if <paramref name="source"/> is null or empty</returns> | |
| public static bool IsNullOrEmpty<T>([NotNullWhen(false)] | |
| this IEnumerable<T>? source) | |
| => source == null || !source.Any(); | |
| ///<summary>Synonym for <see cref="IsNullOrEmpty"/></summary> | |
| /// <returns><c>true</c> if <paramref name="source"/> is null or empty</returns> | |
| public static bool None<T>([NotNullWhen(false)] | |
| this IEnumerable<T>? source) | |
| => source == null || !source.Any(); | |
| /// <returns> | |
| /// <c>true</c> if <paramref name="source"/> is null or | |
| /// no element satisfies <paramref name="predicate"/> | |
| /// </returns> | |
| public static bool None<T>([NotNullWhen(false)] | |
| this IEnumerable<T>? source, | |
| Func<T,bool> predicate) | |
| => source == null || !source.Any(predicate); | |
| /// <returns> | |
| /// <c>false</c> if <paramref name="source"/> is null. | |
| /// <c>true</c> if enumerating <paramref name="source"/> finds at least <paramref name="count"/> rows. | |
| /// An empty list does have at least zero rows. | |
| /// </returns> | |
| public static bool HasCountAtLeast<T>([NotNullWhen(true)] | |
| this IEnumerable<T>? source, int count) | |
| => (source,count) switch | |
| { | |
| (null,_) => false, | |
| (not null,0) => true, | |
| (_,_) => source.Skip(count > 1 ? count - 1 : 0).Any() is true | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment