-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
PagedEnumerableExtensions.cs
126 lines (113 loc) · 6.51 KB
/
PagedEnumerableExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Azure.Batch
{
/// <summary>
/// Provides a set of static (Shared in Visual Basic) methods for working with sequences that
/// implement <see cref="IPagedEnumerable{T}"/>.
/// </summary>
public static class PagedEnumerableExtensions
{
/// <summary>
/// Iterates over an <see cref="IPagedEnumerable{T}"/> sequence, invoking an asynchronous delegate for each element.
/// </summary>
/// <param name="source">The <see cref="IPagedEnumerable{T}"/> to iterate over.</param>
/// <param name="body">The asynchronous delegate to execute for each element in <paramref name="source"/>.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynchronous operation.</param>
/// <returns>A <see cref="System.Threading.Tasks.Task"/> that represents the iteration operation. The task
/// completes when iteration is complete.</returns>
/// <remarks>This method processes elements sequentially, not concurrently. That is, for each element in the
/// sequence, the method awaits the asynchronous delegate before processing the next element.</remarks>
public static async Task ForEachAsync<T>(this IPagedEnumerable<T> source, Func<T, Task> body, CancellationToken cancellationToken = default(CancellationToken))
{
if (source == null)
{
throw new ArgumentNullException("source") ;
}
if (body == null)
{
throw new ArgumentNullException("body");
}
await source.ForEachAsync((t, ct) => body(t), cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Iterates over an <see cref="IPagedEnumerable{T}"/> sequence, invoking an asynchronous delegate for each element.
/// </summary>
/// <param name="source">The <see cref="IPagedEnumerable{T}"/> to iterate over.</param>
/// <param name="body">The asynchronous delegate to execute for each element in <paramref name="source"/>.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynchronous operation.</param>
/// <returns>A <see cref="System.Threading.Tasks.Task"/> that represents the iteration operation. The task
/// completes when iteration is complete.</returns>
/// <remarks>This method processes elements sequentially, not concurrently. That is, for each element in the
/// sequence, the method awaits the asynchronous delegate before processing the next element.</remarks>
public static async Task ForEachAsync<T>(this IPagedEnumerable<T> source, Func<T, CancellationToken, Task> body, CancellationToken cancellationToken = default(CancellationToken))
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (body == null)
{
throw new ArgumentNullException("body");
}
using (IPagedEnumerator<T> enumerator = source.GetPagedEnumerator())
{
while (await enumerator.MoveNextAsync(cancellationToken).ConfigureAwait(false))
{
cancellationToken.ThrowIfCancellationRequested();
await body(enumerator.Current, cancellationToken).ConfigureAwait(false);
}
}
}
/// <summary>
/// Iterates over an <see cref="IPagedEnumerable{T}"/> sequence, invoking a synchronous delegate for each element.
/// </summary>
/// <param name="source">The <see cref="IPagedEnumerable{T}"/> to iterate over.</param>
/// <param name="body">The delegate to execute for each element in <paramref name="source"/>.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynchronous operation.</param>
/// <returns>A <see cref="System.Threading.Tasks.Task"/> that represents the iteration operation. The task
/// completes when iteration is complete.</returns>
/// <remarks>This method processes elements sequentially, not concurrently. That is, for each element in the
/// sequence, the method completes execution of the delegate before processing the next element.</remarks>
public static async Task ForEachAsync<T>(this IPagedEnumerable<T> source, Action<T> body, CancellationToken cancellationToken = default(CancellationToken))
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (body == null)
{
throw new ArgumentNullException("body");
}
using (IPagedEnumerator<T> enumerator = source.GetPagedEnumerator())
{
while (await enumerator.MoveNextAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false))
{
cancellationToken.ThrowIfCancellationRequested();
body(enumerator.Current);
}
}
}
/// <summary>
/// Creates a <see cref="List{T}" /> from an <see cref="IPagedEnumerable{T}"/>.
/// </summary>
/// <param name="source">The <see cref="IPagedEnumerable{T}"/> to create a list from.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynchronous operation.</param>
/// <returns>A <see cref="Task{TResult}"/> that represents the asynchronous operation. The result
/// of the task is a <see cref="List{T}" /> containing all elements of the source sequence.</returns>
public static async Task<List<T>> ToListAsync<T>(this IPagedEnumerable<T> source, CancellationToken cancellationToken = default(CancellationToken))
{
if (source == null)
{
throw new ArgumentNullException("source");
}
List<T> results = new List<T>();
await source.ForEachAsync(item => results.Add(item), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
return results;
}
}
}