iTranslated by AI
Converting IAsyncEnumerable<T> to List<T> in C#
Many Azure SDK methods return results as an AsyncPageable<T> class. Furthermore, calling the AsyncPageable<T>.AsPages method on it provides an IAsyncEnumerable<T> interface result. The IAsyncEnumerable<T> interface is known as an asynchronous stream and allows for efficient data reading, but unfortunately, it does not have a method to convert it directly to a List<T>.
While wondering if there was a good way to handle this, I discovered that System.Linq.Async contains a ToListAsync<T> method. This is a package included in Reactive Extensions.
The ToListAsync<T> method does not support subsequent method chaining by itself. Therefore, you need to connect it using the AsTask or ContinueWith methods. Here is a concrete code example:
var blobContainerClient = new BlobContainerClient("{{connection-string}}", "{{containe-rname}}");
var blobNames = await blobContainerClient
.GetBlobsAsync()
.ToListAsync()
.AsTask()
.ContinueWith(task => task.Result.Select(_ => _.Name));
This is also documented in the Microsoft documentation.
Discussion
ContinueWith メソッドを使わず
または
ではどうでしょうか?
コメントありがとうございます。
そうですね、await を () で囲むのが個人的に好きでなかったのでいつも ContinueWith を使っているのを忘れていました。ご指摘ありがとうございます!