Reputation: 8357
How do I cast an Task<IEnumerable<IMapLocationItem>>
to an IEnumerable<IMapLocationItem>
?
Here is my code that gets a Task<IEnumerable<IMapLocationItem>>
:
public async Task<IEnumerable<IMapLocationItem>> GetAllMapLocationItemsFromUserName(string userName)
{
IEnumerable<IMapLocationItem> mapLocationImageGalleries = await db.mapLocationImageGalleries.Where(m => m.userName.Equals(userName)).ToListAsync();
IEnumerable<IMapLocationItem> mapLocationVideoGalleries = await db.mapLocationVideoGalleries.Where(m => m.userName.Equals(userName)).ToListAsync();
IEnumerable<IMapLocationItem> mapLocationItemsCombined = mapLocationImageGalleries.Concat(mapLocationVideoGalleries);
return mapLocationItemsCombined;
}
I can use the keyword .Result
, but I have read somewhere that this prevents the async
task from being asynchronous, and this method takes a very long time to finish when using the Result keyword.
How is the best way to cast a Task<IEnumerable<IMapLocationItem>>
to an IEnumerable<IMapLocationItem>
?
Thanks in advance
Upvotes: 12
Views: 19024
Reputation: 10958
You can't cast a Task<T>
to T
, they represent different things. A Task<T>
represents a task that in the future will eventually return a T
. If you want to get that result of a task you have several options (assuming t = Task<T>
)
t.Result
blocks the execution until the result is available. This has the unfortunate side effect that it might deadlock in some cases, most notably when combined with async
methods in UI threads.await t
schedules a continuation and runs it once the result is available. Only available with C# 5 and .NET 4.5 or .NET 4 with the Microsoft.Bcl.Async library.t.ContinueWith
schedules a continuation and runs it once the result is available.In general I prefer the use of await
or ContinueWith
, but sometimes using Result
is the easiest, especially if you have access to the source of both the calling and called method and thus can ensure that no deadlock occurs.
Upvotes: 13
Reputation: 7923
From the MSDN website http://msdn.microsoft.com/en-us/library/hh191443.aspx
// Signature specifies Task<TResult>
async Task<int> TaskOfTResult_MethodAsync()
{
int hours;
// . . .
// Return statement specifies an integer result.
return hours;
}
// Calls to TaskOfTResult_MethodAsync
Task<int> returnedTaskTResult = TaskOfTResult_MethodAsync();
int intResult = await returnedTaskTResult;
// or, in a single statement
int intResult = await TaskOfTResult_MethodAsync();
You cannot cast a task to the result. That defeats the purpose of async. What the code above does is calls the function asynchronously, then later in the code requests the result by calling "await". This will wait for the asnyc function to finish (will block the code until that is done) until the result is ready.
The issue is that you cannot predict when something will be done when it is running asynchronously, so instead we tell the Task when we are finally ready to wait for the result. It could be done right away or in 5 minutes. Await will wait for as long as it is necessary for the function to finish.
Upvotes: 15