我已经AsyncPageable<T>并且只想获得列表中的第一个结果。
MS 文档建议使用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);
}
Run Code Online (Sandbox Code Playgroud)
有没有什么有效的方法可以只得到第一个结果?就像是FirstOrDefaultAsync()?
目前我正在使用这段代码来获得第一个结果。
var enumerator = response.Value.GetResultsAsync().GetAsyncEnumerator();
await enumerator.MoveNextAsync();
var result = enumerator.Current;
Run Code Online (Sandbox Code Playgroud)