ToDictionary.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. if (keySelector == null)
  16. throw new ArgumentNullException(nameof(keySelector));
  17. return ToDictionary(source, keySelector, CancellationToken.None);
  18. }
  19. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. if (keySelector == null)
  24. throw new ArgumentNullException(nameof(keySelector));
  25. return source.ToDictionary(keySelector, x => x, EqualityComparer<TKey>.Default, cancellationToken);
  26. }
  27. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. if (keySelector == null)
  32. throw new ArgumentNullException(nameof(keySelector));
  33. if (comparer == null)
  34. throw new ArgumentNullException(nameof(comparer));
  35. return ToDictionary(source, keySelector, comparer, CancellationToken.None);
  36. }
  37. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  38. {
  39. if (source == null)
  40. throw new ArgumentNullException(nameof(source));
  41. if (keySelector == null)
  42. throw new ArgumentNullException(nameof(keySelector));
  43. if (comparer == null)
  44. throw new ArgumentNullException(nameof(comparer));
  45. return source.ToDictionary(keySelector, x => x, comparer, cancellationToken);
  46. }
  47. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  48. {
  49. if (source == null)
  50. throw new ArgumentNullException(nameof(source));
  51. if (keySelector == null)
  52. throw new ArgumentNullException(nameof(keySelector));
  53. if (elementSelector == null)
  54. throw new ArgumentNullException(nameof(elementSelector));
  55. return ToDictionary(source, keySelector, elementSelector, CancellationToken.None);
  56. }
  57. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken)
  58. {
  59. if (source == null)
  60. throw new ArgumentNullException(nameof(source));
  61. if (keySelector == null)
  62. throw new ArgumentNullException(nameof(keySelector));
  63. if (elementSelector == null)
  64. throw new ArgumentNullException(nameof(elementSelector));
  65. return source.ToDictionary(keySelector, elementSelector, EqualityComparer<TKey>.Default, cancellationToken);
  66. }
  67. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  68. {
  69. if (source == null)
  70. throw new ArgumentNullException(nameof(source));
  71. if (keySelector == null)
  72. throw new ArgumentNullException(nameof(keySelector));
  73. if (elementSelector == null)
  74. throw new ArgumentNullException(nameof(elementSelector));
  75. if (comparer == null)
  76. throw new ArgumentNullException(nameof(comparer));
  77. return ToDictionary(source, keySelector, elementSelector, comparer, CancellationToken.None);
  78. }
  79. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  80. {
  81. if (source == null)
  82. throw new ArgumentNullException(nameof(source));
  83. if (keySelector == null)
  84. throw new ArgumentNullException(nameof(keySelector));
  85. if (elementSelector == null)
  86. throw new ArgumentNullException(nameof(elementSelector));
  87. if (comparer == null)
  88. throw new ArgumentNullException(nameof(comparer));
  89. return source.Aggregate(
  90. new Dictionary<TKey, TElement>(comparer),
  91. (d, x) =>
  92. {
  93. d.Add(keySelector(x), elementSelector(x));
  94. return d;
  95. },
  96. cancellationToken
  97. );
  98. }
  99. }
  100. }