EnumerableExtensions.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Abp.Collections.Extensions
  5. {
  6. /// <summary>
  7. /// Extension methods for <see cref="IEnumerable{T}"/>.
  8. /// </summary>
  9. public static class EnumerableExtensions
  10. {
  11. /// <summary>
  12. /// Concatenates the members of a constructed <see cref="IEnumerable{T}"/> collection of type System.String, using the specified separator between each member.
  13. /// This is a shortcut for string.Join(...)
  14. /// </summary>
  15. /// <param name="source">A collection that contains the strings to concatenate.</param>
  16. /// <param name="separator">The string to use as a separator. separator is included in the returned string only if values has more than one element.</param>
  17. /// <returns>A string that consists of the members of values delimited by the separator string. If values has no members, the method returns System.String.Empty.</returns>
  18. public static string JoinAsString(this IEnumerable<string> source, string separator)
  19. {
  20. return string.Join(separator, source);
  21. }
  22. /// <summary>
  23. /// Concatenates the members of a collection, using the specified separator between each member.
  24. /// This is a shortcut for string.Join(...)
  25. /// </summary>
  26. /// <param name="source">A collection that contains the objects to concatenate.</param>
  27. /// <param name="separator">The string to use as a separator. separator is included in the returned string only if values has more than one element.</param>
  28. /// <typeparam name="T">The type of the members of values.</typeparam>
  29. /// <returns>A string that consists of the members of values delimited by the separator string. If values has no members, the method returns System.String.Empty.</returns>
  30. public static string JoinAsString<T>(this IEnumerable<T> source, string separator)
  31. {
  32. return string.Join(separator, source);
  33. }
  34. /// <summary>
  35. /// Filters a <see cref="IEnumerable{T}"/> by given predicate if given condition is true.
  36. /// </summary>
  37. /// <param name="source">Enumerable to apply filtering</param>
  38. /// <param name="condition">A boolean value</param>
  39. /// <param name="predicate">Predicate to filter the enumerable</param>
  40. /// <returns>Filtered or not filtered enumerable based on <paramref name="condition"/></returns>
  41. public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> source, bool condition, Func<T, bool> predicate)
  42. {
  43. return condition
  44. ? source.Where(predicate)
  45. : source;
  46. }
  47. /// <summary>
  48. /// Filters a <see cref="IEnumerable{T}"/> by given predicate if given condition is true.
  49. /// </summary>
  50. /// <param name="source">Enumerable to apply filtering</param>
  51. /// <param name="condition">A boolean value</param>
  52. /// <param name="predicate">Predicate to filter the enumerable</param>
  53. /// <returns>Filtered or not filtered enumerable based on <paramref name="condition"/></returns>
  54. public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> source, bool condition, Func<T, int, bool> predicate)
  55. {
  56. return condition
  57. ? source.Where(predicate)
  58. : source;
  59. }
  60. }
  61. }