|
|
@@ -66,62 +66,6 @@ namespace System.Linq
|
|
|
return source.Where(predicate).Last(cancellationToken);
|
|
|
}
|
|
|
|
|
|
- public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source)
|
|
|
- {
|
|
|
- if (source == null)
|
|
|
- throw new ArgumentNullException(nameof(source));
|
|
|
-
|
|
|
- return LastOrDefault(source, CancellationToken.None);
|
|
|
- }
|
|
|
-
|
|
|
- public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
|
|
- {
|
|
|
- if (source == null)
|
|
|
- throw new ArgumentNullException(nameof(source));
|
|
|
-
|
|
|
- return LastOrDefault_(source, cancellationToken);
|
|
|
- }
|
|
|
-
|
|
|
- public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
|
|
|
- {
|
|
|
- if (source == null)
|
|
|
- throw new ArgumentNullException(nameof(source));
|
|
|
- if (predicate == null)
|
|
|
- throw new ArgumentNullException(nameof(predicate));
|
|
|
-
|
|
|
- return LastOrDefault(source, predicate, CancellationToken.None);
|
|
|
- }
|
|
|
-
|
|
|
- public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
|
|
|
- {
|
|
|
- if (source == null)
|
|
|
- throw new ArgumentNullException(nameof(source));
|
|
|
- if (predicate == null)
|
|
|
- throw new ArgumentNullException(nameof(predicate));
|
|
|
-
|
|
|
- return source.Where(predicate).LastOrDefault(cancellationToken);
|
|
|
- }
|
|
|
-
|
|
|
- public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
|
|
|
- {
|
|
|
- if (source == null)
|
|
|
- throw new ArgumentNullException(nameof(source));
|
|
|
- if (predicate == null)
|
|
|
- throw new ArgumentNullException(nameof(predicate));
|
|
|
-
|
|
|
- return LastOrDefault(source, predicate, CancellationToken.None);
|
|
|
- }
|
|
|
-
|
|
|
- public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
|
|
|
- {
|
|
|
- if (source == null)
|
|
|
- throw new ArgumentNullException(nameof(source));
|
|
|
- if (predicate == null)
|
|
|
- throw new ArgumentNullException(nameof(predicate));
|
|
|
-
|
|
|
- return source.Where(predicate).LastOrDefault(cancellationToken);
|
|
|
- }
|
|
|
-
|
|
|
private static async Task<TSource> Last_<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var last = default(TSource);
|
|
|
@@ -156,37 +100,5 @@ namespace System.Linq
|
|
|
|
|
|
return last;
|
|
|
}
|
|
|
-
|
|
|
- private static async Task<TSource> LastOrDefault_<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
|
|
- {
|
|
|
- var last = default(TSource);
|
|
|
- var hasLast = false;
|
|
|
-
|
|
|
- if (source is IList<TSource> list)
|
|
|
- {
|
|
|
- var count = list.Count;
|
|
|
- if (count > 0)
|
|
|
- {
|
|
|
- return list[count - 1];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- var e = source.GetAsyncEnumerator();
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- while (await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
|
|
|
- {
|
|
|
- hasLast = true;
|
|
|
- last = e.Current;
|
|
|
- }
|
|
|
- }
|
|
|
- finally
|
|
|
- {
|
|
|
- await e.DisposeAsync().ConfigureAwait(false);
|
|
|
- }
|
|
|
-
|
|
|
- return !hasLast ? default(TSource) : last;
|
|
|
- }
|
|
|
}
|
|
|
}
|