In this article, you'll learn how to use the Azure SDK for .NET pagination functionality to work efficiently and productively with large data sets. Pagination is the act of dividing large data sets into pages, making it easier for the consumer to iterate through smaller amounts of data. Starting with C# 8, you can create and consume streams asynchronously using Asynchronous (async) streams. Async streams are based on the IAsyncEnumerable<T> interface. The Azure SDK for .NET exposes an implementation of IAsyncEnumerable<T> with its AsyncPageable<T> class.
All of the samples in this article rely on the following NuGet packages:
A collection of values asynchronously retrieved in pages
Most of the samples in this article are asynchronous, using variations of the AsyncPageable<T> type. Using asynchronous programming for I/O-bound operations is ideal. A perfect use case is using the async APIs from the Azure SDK for .NET as these operations represent HTTP/S network calls.
Iterate over AsyncPageable with await foreach
To iterate over an AsyncPageable<T> using the await foreach syntax, consider the following example:
The MoveNextAsync() method is invoked repeatedly until there are no items to return.
Iterate over AsyncPageable pages
If you want control over receiving pages of values from the service, use the AsyncPageable<T>.AsPages method:
C#
async Task IterateSecretsAsPagesAsync()
{
AsyncPageable<SecretProperties> allSecrets = client.GetPropertiesOfSecretsAsync();
awaitforeach (Page<SecretProperties> page in allSecrets.AsPages())
{
foreach (SecretProperties secret in page.Values)
{
Console.WriteLine($"IterateSecretsAsPagesAsync: {secret.Name}");
}
// The continuation token that can be used in AsPages call to resume enumeration
Console.WriteLine(page.ContinuationToken);
}
}
System.Linq.Async provides other methods that provide functionality equivalent to their synchronous Enumerable counterparts. Examples of such methods include Select, Where, OrderBy, and GroupBy.
Beware client-side evaluation
When using the System.Linq.Async package, beware that LINQ operations are executed on the client. The following query would fetch all the items just to count them:
The same warning applies to operators like Where. Always prefer server-side filtering, aggregation, or projections of data if available.
As an observable sequence
The System.Linq.Async package is primarily used to provide observer pattern capabilities over IAsyncEnumerable<T> sequences. Asynchronous streams are pull-based. As their items are iterated over, the next available item is pulled. This approach is in juxtaposition with the observer pattern, which is push-based. As items become available, they're pushed to subscribers who act as observers. The System.Linq.Async package provides the ToObservable extension method that lets you convert an IAsyncEnumerable<T> to an IObservable<T>.
Imagine an IObserver<SecretProperties> implementation:
ამ შიგთავსის წყაროს მოძიება GitHub-ზე არის შესაძლებელი, სადაც თქვენ ასევე შეგიძლიათ პრობლემების შექმნა და განხილვა და მოთხოვნების გადმოტანა. დამატებითი ინფორმაციისთვის იხილეთ ჩვენი დამხმარე სახელმძღვანელო.
.NET-(ი)ს უკუკავშირი
.NET არის ღია წყაროს პროექტი. აირჩიეთ ბმული უკუკავშირის გასაგზავნად:
შემოუერთდით Meetup სერიას, რათა შექმნათ მასშტაბური AI გადაწყვეტილებები რეალურ სამყაროში გამოყენების შემთხვევებზე დაყრდნობით თანამემამულე დეველოპერებთან და ექსპერტებთან.