I'm trying to use the LINQ IEnumerable.Aggregate function to create a string consisting of files retrieved through async calls. Not a hundred percent sure that it's possible, and I'm also aware that there are other solutions, but I'd like to give it a try.
For now my code looks like this:
private static async Task<string> GetFiles(IEnumerable<string> filePaths)
{
return filePaths.Aggregate(async (current, path) => current + await GetFile(path));
}
But the "async" inside the method call is error marked saying "the return of an async method must be void, Task, or Task". I get that error in general, but I'm not sure how to arrange this specific case to avoid it. Any ideas?
UPDATE:
Just to clarify, the GetFile() method is indeed asynchronous and returns Task<string>
:
private static async Task<string> GetFile(string filePath) { ... }
No need to get into the specific code, but for those interested it uses HttpClient.GetAsync(filePath)
and the returns its response.Content.ReadAsStringAsync().Result
.