I have AsyncPageable<T> and want to get only the first result from the list.
MS docs suggests using await foreach
// call a service method, which returns AsyncPageable<T>
AsyncPageable<SecretProperties> allSecretProperties = client.GetPropertiesOfSecretsAsync();
await foreach (SecretProperties secretProperties in allSecretProperties)
{
Console.WriteLine(secretProperties.Name);
}
Is there any efficient way to get only the first result? Something like FirstOrDefaultAsync()?
At the moment I am using this code to get the first result.
var enumerator = response.Value.GetResultsAsync().GetAsyncEnumerator();
await enumerator.MoveNextAsync();
var result = enumerator.Current;
Since AsyncPageable<T> implements IAsyncEnumerable<T>, you can install System.Linq.Async nuget and use the methods it provides:
var result = await allSecretProperties.FirstOrDefaultAsync();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With