Min.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. namespace System.Linq
  6. {
  7. public static partial class EnumerableEx
  8. {
  9. /// <summary>
  10. /// Returns the minimum value in the enumerable sequence by using the specified comparer to compare values.
  11. /// </summary>
  12. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  13. /// <param name="source">Source sequence.</param>
  14. /// <param name="comparer">Comparer used to determine the minimum value.</param>
  15. /// <returns>Minimum value in the sequence.</returns>
  16. public static TSource Min<TSource>(this IEnumerable<TSource> source, IComparer<TSource> comparer)
  17. {
  18. if (source == null)
  19. {
  20. throw new ArgumentNullException(nameof(source));
  21. }
  22. if (comparer == null)
  23. {
  24. throw new ArgumentNullException(nameof(comparer));
  25. }
  26. return Extrema(source, x => x, comparer, -1);
  27. }
  28. /// <summary>
  29. /// Returns the elements with the minimum key value by using the default comparer to compare key values.
  30. /// </summary>
  31. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  32. /// <typeparam name="TKey">Key type.</typeparam>
  33. /// <param name="source">Source sequence.</param>
  34. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  35. /// <returns>List with the elements that share the same minimum key value.</returns>
  36. public static IList<TSource> MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  37. {
  38. if (source == null)
  39. {
  40. throw new ArgumentNullException(nameof(source));
  41. }
  42. if (keySelector == null)
  43. {
  44. throw new ArgumentNullException(nameof(keySelector));
  45. }
  46. return MinBy(source, keySelector, Comparer<TKey>.Default);
  47. }
  48. /// <summary>
  49. /// Returns the elements with the minimum key value by using the specified comparer to compare key values.
  50. /// </summary>
  51. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  52. /// <typeparam name="TKey">Key type.</typeparam>
  53. /// <param name="source">Source sequence.</param>
  54. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  55. /// <param name="comparer">Comparer used to determine the minimum key value.</param>
  56. /// <returns>List with the elements that share the same minimum key value.</returns>
  57. public static IList<TSource> MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  58. {
  59. if (source == null)
  60. {
  61. throw new ArgumentNullException(nameof(source));
  62. }
  63. if (keySelector == null)
  64. {
  65. throw new ArgumentNullException(nameof(keySelector));
  66. }
  67. if (comparer == null)
  68. {
  69. throw new ArgumentNullException(nameof(comparer));
  70. }
  71. return ExtremaBy(source, keySelector, comparer, -1);
  72. }
  73. }
  74. }