LinqExtensions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Dynamic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace EasyDevCore.Common
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. public static class LinqExtensions
  13. {
  14. /// <summary>
  15. /// Indexes the of.
  16. /// </summary>
  17. /// <typeparam name="TSource">The type of the source.</typeparam>
  18. /// <param name="source">The source.</param>
  19. /// <param name="item">The item.</param>
  20. /// <returns></returns>
  21. public static int IndexOf<TSource>(this IEnumerable<TSource> source, TSource item)
  22. {
  23. int index = 0;
  24. foreach (TSource sourceItem in source)
  25. {
  26. if ((item == null && sourceItem == null) || (sourceItem != null && sourceItem.Equals(item))) return index;
  27. index++;
  28. }
  29. return -1;
  30. }
  31. /// <summary>
  32. /// Fors the each.
  33. /// </summary>
  34. /// <typeparam name="TSource">The type of the source.</typeparam>
  35. /// <param name="source">The source.</param>
  36. /// <param name="action">The action.</param>
  37. /// <returns></returns>
  38. public static IEnumerable<TSource> ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource> action)
  39. {
  40. foreach (var item in source)
  41. action(item);
  42. return source;
  43. }
  44. /// <summary>
  45. /// Fors the each.
  46. /// </summary>
  47. /// <typeparam name="TSource">The type of the source.</typeparam>
  48. /// <param name="source">The source.</param>
  49. /// <param name="action">The action.</param>
  50. /// <returns></returns>
  51. public static IEnumerable ForEach<TSource>(this IEnumerable source, Action<TSource> action)
  52. {
  53. foreach (var item in source)
  54. action((TSource)item);
  55. return source;
  56. }
  57. /// <summary>
  58. /// Firsts the value.
  59. /// </summary>
  60. /// <typeparam name="TSource">The type of the t source.</typeparam>
  61. /// <typeparam name="V"></typeparam>
  62. /// <param name="source">The source.</param>
  63. /// <param name="predicate">The predicate.</param>
  64. /// <param name="selector">The selector.</param>
  65. /// <returns>V.</returns>
  66. public static V FirstValue<TSource, V>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, V> selector)
  67. {
  68. #pragma warning disable CS8604 // Possible null reference argument.
  69. return selector(source.FirstOrDefault(predicate));
  70. #pragma warning restore CS8604 // Possible null reference argument.
  71. }
  72. /// <summary>
  73. /// Firsts the value.
  74. /// </summary>
  75. /// <typeparam name="TSource">The type of the t source.</typeparam>
  76. /// <typeparam name="V"></typeparam>
  77. /// <param name="source">The source.</param>
  78. /// <param name="selector">The selector.</param>
  79. /// <returns>V.</returns>
  80. public static V FirstValue<TSource, V>(this IEnumerable<TSource> source, Func<TSource, V> selector)
  81. {
  82. #pragma warning disable CS8604 // Possible null reference argument.
  83. return selector(source.FirstOrDefault());
  84. #pragma warning restore CS8604 // Possible null reference argument.
  85. }
  86. /// <summary>
  87. /// Maps the specified selector.
  88. /// </summary>
  89. /// <typeparam name="TSource">The type of the source.</typeparam>
  90. /// <typeparam name="TResult">The type of the result.</typeparam>
  91. /// <param name="source">The source.</param>
  92. /// <param name="selector">The selector.</param>
  93. /// <returns>
  94. /// <br />
  95. /// </returns>
  96. public static IEnumerable<TResult> Map<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
  97. => source.Select(selector);
  98. }
  99. }