IEnumerableExtensions.cs 968 B

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Core.Util.Extension
  6. {
  7. public static class IEnumerableExtensions
  8. {
  9. public static IEnumerable<T> Safe<T>(this IEnumerable<T> collection)
  10. {
  11. return collection ?? Enumerable.Empty<T>();
  12. }
  13. public static bool Contains<T>(this IEnumerable<T> collection, Predicate<T> condition)
  14. {
  15. return collection.Any(x => condition(x));
  16. }
  17. public static bool IsEmpty<T>(this IEnumerable<T> collection)
  18. {
  19. if (collection == null)
  20. return true;
  21. var coll = collection as ICollection;
  22. if (coll != null)
  23. return coll.Count == 0;
  24. return !collection.Any();
  25. }
  26. public static bool IsNotEmpty<T>(this IEnumerable<T> collection)
  27. {
  28. return !IsEmpty(collection);
  29. }
  30. }
  31. }