小编and*_*ner的帖子

AsyncPageable<T> 异步获取第一个结果

我已经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)

c# async-await

9
推荐指数
2
解决办法
7787
查看次数

标签 统计

async-await ×1

c# ×1