I am working on converting some synchronous code to async in C# (.NET 4.5). I have a senario where I need to do an operation using the number of models retrieved by a remote query as an input.
An example of the desired behavior:
public async Task<List<Model>> GetEntityModelsAsync()
{
return // some models from a database async
}
public async Task<int> DoSomeWorkWithModelCount()
{
Task<IEnumerable<Model>> models = GetEntityModelsAsync();
// some other code to execute before awaiting
int count = await models.Count();
// do more work and return result
}
The above code will not compile with the error 'Task<IEnumerable>' does not contain a definition for 'Count'...
I assume this is because the type of the variable is 'Task<IEnumerable>'. Is there any way to extract the result from the task and do operations that are allowed on its type?
I have tried getting the result in this way:
int count = await models.Result.Count(); // error: 'int' does not contain a definition for 'GetAwaiter'
await recentApplications.GetAwaiter().GetResult(); // error: 'int' does not contain a definition for 'GetAwaiter'
Am I forced to first await the result into another variable like this:
IEnumerable<Model> modelsResult = await models;
int count = modelsResult.Count();
Or is there a more streamlined way to do type operations on the results of awaited tasks?