OrderBy.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  10. public static partial class AsyncEnumerable
  11. {
  12. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  13. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.orderby?view=net-9.0-pp#system-linq-asyncenumerable-orderby-2(system-collections-generic-iasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
  14. // The method linked to above includes a comparer parameter with a default value of null.
  15. /// <summary>
  16. /// Sorts the elements of a sequence in ascending order according to a key.
  17. /// </summary>
  18. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  19. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  20. /// <param name="source">An async-enumerable sequence of values to order.</param>
  21. /// <param name="keySelector">A function to extract a key from an element.</param>
  22. /// <returns>An ordered async-enumerable sequence whose elements are sorted according to a key.</returns>
  23. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  24. public static IOrderedAsyncEnumerable<TSource> OrderBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector) =>
  25. new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer: null, descending: false, parent: null);
  26. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  27. /// <summary>
  28. /// Sorts the elements of a sequence in ascending order according to a key obtained by invoking a transform function on each element and awaiting the result.
  29. /// </summary>
  30. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  31. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  32. /// <param name="source">An async-enumerable sequence of values to order.</param>
  33. /// <param name="keySelector">An asynchronous function to extract a key from an element.</param>
  34. /// <returns>An ordered async-enumerable sequence whose elements are sorted according to a key.</returns>
  35. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  36. [GenerateAsyncOverload]
  37. [Obsolete("Use OrderBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByAwait functionality now exists as overloads of OrderBy.")]
  38. private static IOrderedAsyncEnumerable<TSource> OrderByAwaitCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector) =>
  39. new OrderedAsyncEnumerableWithTask<TSource, TKey>(source, keySelector, comparer: null, descending: false, parent: null);
  40. #if !NO_DEEP_CANCELLATION
  41. [GenerateAsyncOverload]
  42. [Obsolete("Use OrderBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByAwaitWithCancellation functionality now exists as overloads of OrderBy.")]
  43. private static IOrderedAsyncEnumerable<TSource> OrderByAwaitWithCancellationCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector) =>
  44. new OrderedAsyncEnumerableWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer: null, descending: false, parent: null);
  45. #endif
  46. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  47. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.orderby?view=net-9.0-pp#system-linq-asyncenumerable-orderby-2(system-collections-generic-iasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
  48. /// <summary>
  49. /// Sorts the elements of a sequence in ascending order by using a specified comparer.
  50. /// </summary>
  51. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  52. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  53. /// <param name="source">An async-enumerable sequence of values to order.</param>
  54. /// <param name="keySelector">A function to extract a key from an element.</param>
  55. /// <param name="comparer">A comparer to compare keys.</param>
  56. /// <returns>An ordered async-enumerable sequence whose elements are sorted according to a key.</returns>
  57. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  58. public static IOrderedAsyncEnumerable<TSource> OrderBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer) =>
  59. new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer, descending: false, parent: null);
  60. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  61. /// <summary>
  62. /// Sorts the elements of a sequence in ascending order by using a specified comparer. The keys are obtained by invoking the transform function on each element and awaiting the result.
  63. /// </summary>
  64. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  65. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  66. /// <param name="source">An async-enumerable sequence of values to order.</param>
  67. /// <param name="keySelector">An asynchronous function to extract a key from an element.</param>
  68. /// <param name="comparer">A comparer to compare keys.</param>
  69. /// <returns>An ordered async-enumerable sequence whose elements are sorted according to a key.</returns>
  70. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  71. [GenerateAsyncOverload]
  72. [Obsolete("Use OrderBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByAwait functionality now exists as overloads of OrderBy.")]
  73. private static IOrderedAsyncEnumerable<TSource> OrderByAwaitCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer) =>
  74. new OrderedAsyncEnumerableWithTask<TSource, TKey>(source, keySelector, comparer, descending: false, parent: null);
  75. #if !NO_DEEP_CANCELLATION
  76. [GenerateAsyncOverload]
  77. [Obsolete("Use OrderBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByAwaitWithCancellation functionality now exists as overloads of OrderBy.")]
  78. private static IOrderedAsyncEnumerable<TSource> OrderByAwaitWithCancellationCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer) =>
  79. new OrderedAsyncEnumerableWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer, descending: false, parent: null);
  80. #endif
  81. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  82. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.orderbydescending?view=net-9.0-pp#system-linq-asyncenumerable-orderbydescending-2(system-collections-generic-iasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
  83. // The method linked to above includes a comparer parameter with a default value of null.
  84. /// <summary>
  85. /// Sorts the elements of a sequence in descending order according to a key.
  86. /// </summary>
  87. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  88. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  89. /// <param name="source">An async-enumerable sequence of values to order.</param>
  90. /// <param name="keySelector">A function to extract a key from an element.</param>
  91. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  92. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  93. public static IOrderedAsyncEnumerable<TSource> OrderByDescending<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector) =>
  94. new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer: null, descending: true, parent: null);
  95. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  96. /// <summary>
  97. /// Sorts the elements of a sequence in descending order according to a key obtained by invoking a transform function on each element and awaiting the result.
  98. /// </summary>
  99. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  100. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  101. /// <param name="source">An async-enumerable sequence of values to order.</param>
  102. /// <param name="keySelector">An asynchronous function to extract a key from an element.</param>
  103. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  104. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  105. [GenerateAsyncOverload]
  106. [Obsolete("Use OrderByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByDescendingAwait functionality now exists as overloads of OrderByDescending.")]
  107. private static IOrderedAsyncEnumerable<TSource> OrderByDescendingAwaitCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector) =>
  108. new OrderedAsyncEnumerableWithTask<TSource, TKey>(source, keySelector, comparer: null, descending: true, parent: null);
  109. #if !NO_DEEP_CANCELLATION
  110. [GenerateAsyncOverload]
  111. [Obsolete("Use OrderByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByDescendingAwaitWithCancellation functionality now exists as overloads of OrderByDescending.")]
  112. private static IOrderedAsyncEnumerable<TSource> OrderByDescendingAwaitWithCancellationCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector) =>
  113. new OrderedAsyncEnumerableWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer: null, descending: true, parent: null);
  114. #endif
  115. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  116. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.orderbydescending?view=net-9.0-pp#system-linq-asyncenumerable-orderbydescending-2(system-collections-generic-iasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
  117. /// <summary>
  118. /// Sorts the elements of a sequence in descending order by using a specified comparer.
  119. /// </summary>
  120. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  121. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  122. /// <param name="source">An async-enumerable sequence of values to order.</param>
  123. /// <param name="keySelector">A function to extract a key from an element.</param>
  124. /// <param name="comparer">A comparer to compare keys.</param>
  125. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  126. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  127. public static IOrderedAsyncEnumerable<TSource> OrderByDescending<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer) =>
  128. new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer, descending: true, parent: null);
  129. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  130. /// <summary>
  131. /// Sorts the elements of a sequence in descending order by using a specified comparer. The keys are obtained by invoking the transform function on each element and awaiting the result.
  132. /// </summary>
  133. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  134. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  135. /// <param name="source">An async-enumerable sequence of values to order.</param>
  136. /// <param name="keySelector">An asynchronous function to extract a key from an element.</param>
  137. /// <param name="comparer">A comparer to compare keys.</param>
  138. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  139. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  140. [GenerateAsyncOverload]
  141. [Obsolete("Use OrderByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByDescendingAwait functionality now exists as overloads of OrderByDescending.")]
  142. private static IOrderedAsyncEnumerable<TSource> OrderByDescendingAwaitCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer) =>
  143. new OrderedAsyncEnumerableWithTask<TSource, TKey>(source, keySelector, comparer, descending: true, parent: null);
  144. #if !NO_DEEP_CANCELLATION
  145. [GenerateAsyncOverload]
  146. [Obsolete("Use OrderByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByDescendingAwaitWithCancellation functionality now exists as overloads of OrderByDescending.")]
  147. private static IOrderedAsyncEnumerable<TSource> OrderByDescendingAwaitWithCancellationCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer) =>
  148. new OrderedAsyncEnumerableWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer, descending: true, parent: null);
  149. #endif
  150. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  151. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.thenby?view=net-9.0-pp#system-linq-asyncenumerable-thenby-2(system-linq-iorderedasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
  152. // The method linked to above includes a comparer parameter with a default value of null.
  153. /// <summary>
  154. /// Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.
  155. /// </summary>
  156. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  157. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  158. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  159. /// <param name="keySelector">A function to extract a key from each element.</param>
  160. /// <returns>An ordered async-enumerable whose elements are sorted according to a key.</returns>
  161. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  162. public static IOrderedAsyncEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  163. {
  164. if (source == null)
  165. throw Error.ArgumentNull(nameof(source));
  166. return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: false);
  167. }
  168. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  169. /// <summary>
  170. /// Performs a subsequent ordering of the elements in a sequence in ascending order according to a key obtained by invoking a transform function on each element and awaiting the result.
  171. /// </summary>
  172. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  173. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  174. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  175. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  176. /// <returns>An ordered async-enumerable whose elements are sorted according to a key.</returns>
  177. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  178. [GenerateAsyncOverload]
  179. [Obsolete("Use ThenBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByAwait functionality now exists as overloads of ThenBy.")]
  180. private static IOrderedAsyncEnumerable<TSource> ThenByAwaitCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector)
  181. {
  182. if (source == null)
  183. throw Error.ArgumentNull(nameof(source));
  184. return source.CreateOrderedEnumerable(keySelector, comparer: default(IComparer<TKey>), descending: false);
  185. }
  186. #if !NO_DEEP_CANCELLATION
  187. [GenerateAsyncOverload]
  188. [Obsolete("Use ThenBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByAwaitWithCancellation functionality now exists as overloads of ThenBy.")]
  189. private static IOrderedAsyncEnumerable<TSource> ThenByAwaitWithCancellationCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector)
  190. {
  191. if (source == null)
  192. throw Error.ArgumentNull(nameof(source));
  193. return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: false);
  194. }
  195. #endif
  196. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  197. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.thenby?view=net-9.0-pp#system-linq-asyncenumerable-thenby-2(system-linq-iorderedasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
  198. /// <summary>
  199. /// Performs a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer.
  200. /// </summary>
  201. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  202. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  203. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  204. /// <param name="keySelector">A function to extract a key from each element.</param>
  205. /// <param name="comparer">A comparer to compare keys.</param>
  206. /// <returns>An ordered async-enumerable whose elements are sorted according to a key.</returns>
  207. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  208. public static IOrderedAsyncEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  209. {
  210. if (source == null)
  211. throw Error.ArgumentNull(nameof(source));
  212. return source.CreateOrderedEnumerable(keySelector, comparer, descending: false);
  213. }
  214. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  215. /// <summary>
  216. /// Performs a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer. The keys are obtained by invoking a transform function on each element and awaiting the result.
  217. /// </summary>
  218. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  219. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  220. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  221. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  222. /// <param name="comparer">A comparer to compare keys.</param>
  223. /// <returns>An ordered async-enumerable whose elements are sorted according to a key.</returns>
  224. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  225. [GenerateAsyncOverload]
  226. [Obsolete("Use ThenBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByAwait functionality now exists as overloads of ThenBy.")]
  227. private static IOrderedAsyncEnumerable<TSource> ThenByAwaitCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer)
  228. {
  229. if (source == null)
  230. throw Error.ArgumentNull(nameof(source));
  231. return source.CreateOrderedEnumerable(keySelector, comparer, descending: false);
  232. }
  233. #if !NO_DEEP_CANCELLATION
  234. [GenerateAsyncOverload]
  235. [Obsolete("Use ThenBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByAwaitWithCancellation functionality now exists as overloads of ThenBy.")]
  236. private static IOrderedAsyncEnumerable<TSource> ThenByAwaitWithCancellationCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer)
  237. {
  238. if (source == null)
  239. throw Error.ArgumentNull(nameof(source));
  240. return source.CreateOrderedEnumerable(keySelector, comparer, descending: false);
  241. }
  242. #endif
  243. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  244. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.thenbydescending?view=net-9.0-pp#system-linq-asyncenumerable-thenbydescending-2(system-linq-iorderedasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
  245. // The method linked to above includes a comparer parameter with a default value of null.
  246. /// <summary>
  247. /// Performs a subsequent ordering of the elements in a sequence in descending order, according to a key.
  248. /// </summary>
  249. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  250. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  251. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  252. /// <param name="keySelector">A function to extract a key from each element.</param>
  253. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  254. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  255. public static IOrderedAsyncEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  256. {
  257. if (source == null)
  258. throw Error.ArgumentNull(nameof(source));
  259. return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: true);
  260. }
  261. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  262. /// <summary>
  263. /// Performs a subsequent ordering of the elements in a sequence in descending order, according to a key obtained by invoking a transform function on each element and awaiting the result.
  264. /// </summary>
  265. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  266. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  267. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  268. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  269. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  270. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  271. [GenerateAsyncOverload]
  272. [Obsolete("Use ThenByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByDescendingAwait functionality now exists as overloads of ThenByDescending.")]
  273. private static IOrderedAsyncEnumerable<TSource> ThenByDescendingAwaitCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector)
  274. {
  275. if (source == null)
  276. throw Error.ArgumentNull(nameof(source));
  277. return source.CreateOrderedEnumerable(keySelector, comparer: default(IComparer<TKey>), descending: true);
  278. }
  279. #if !NO_DEEP_CANCELLATION
  280. [GenerateAsyncOverload]
  281. [Obsolete("Use ThenByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByDescendingAwaitWithCancellation functionality now exists as overloads of ThenByDescending.")]
  282. private static IOrderedAsyncEnumerable<TSource> ThenByDescendingAwaitWithCancellationCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector)
  283. {
  284. if (source == null)
  285. throw Error.ArgumentNull(nameof(source));
  286. return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: true);
  287. }
  288. #endif
  289. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  290. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.thenbydescending?view=net-9.0-pp#system-linq-asyncenumerable-thenbydescending-2(system-linq-iorderedasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
  291. /// <summary>
  292. /// Performs a subsequent ordering of the elements in a sequence in descending order by using a specified comparer.
  293. /// </summary>
  294. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  295. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  296. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  297. /// <param name="keySelector">A function to extract a key from each element.</param>
  298. /// <param name="comparer">A comparer to compare keys.</param>
  299. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  300. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  301. public static IOrderedAsyncEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  302. {
  303. if (source == null)
  304. throw Error.ArgumentNull(nameof(source));
  305. return source.CreateOrderedEnumerable(keySelector, comparer, descending: true);
  306. }
  307. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  308. /// <summary>
  309. /// Performs a subsequent ordering of the elements in a sequence in descending order by using a specified comparer. The keys are obtained by invoking a transform function on each element and awaiting the result.
  310. /// </summary>
  311. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  312. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  313. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  314. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  315. /// <param name="comparer">A comparer to compare keys.</param>
  316. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  317. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  318. [GenerateAsyncOverload]
  319. [Obsolete("Use ThenByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByDescendingAwait functionality now exists as overloads of ThenByDescending.")]
  320. private static IOrderedAsyncEnumerable<TSource> ThenByDescendingAwaitCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer)
  321. {
  322. if (source == null)
  323. throw Error.ArgumentNull(nameof(source));
  324. return source.CreateOrderedEnumerable(keySelector, comparer, descending: true);
  325. }
  326. #if !NO_DEEP_CANCELLATION
  327. [GenerateAsyncOverload]
  328. [Obsolete("Use ThenByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByDescendingAwaitWithCancellation functionality now exists as overloads of ThenByDescending.")]
  329. private static IOrderedAsyncEnumerable<TSource> ThenByDescendingAwaitWithCancellationCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer)
  330. {
  331. if (source == null)
  332. throw Error.ArgumentNull(nameof(source));
  333. return source.CreateOrderedEnumerable(keySelector, comparer, descending: true);
  334. }
  335. #endif
  336. }
  337. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  338. }