Reputation: 517
I have the following method:
public async IEnumerable<string> GetListDriversAsync()
{
var drives = await graphClient.Drive.Root.Children.Request().GetAsync();
foreach (var d in drives)
yield return d.ToString();
}
But compiler error says:
"The return type of an async must be void, Task or Task
<T>
"
How do I return IEnumerable when the method is async?
Upvotes: 6
Views: 10365
Reputation: 10851
An alternative way is possible in C# 8. It uses IAsyncEnumerable.
public async IAsyncEnumerable<string> GetListDriversAsync()
{
var drives = await graphClient.Drive.Root.Children.Request().GetAsync();
foreach (var d in drives)
yield return d.ToString();
}
It will alter your signature a bit, which may (or may not) be an option for you.
Usage:
await foreach (var driver in foo.GetListDriversAsync())
{
Console.WriteLine(driver );
}
Upvotes: 10
Reputation: 117134
Try this:
public async Task<IEnumerable<string>> GetListDriversAsync()
{
var drives = await graphClient.Drive.Root.Children.Request().GetAsync();
IEnumerable<string> GetListDrivers()
{
foreach (var d in drives)
yield return d.ToString();
}
return GetListDrivers();
}
Upvotes: 8