相关文章推荐
笑点低的毛豆  ·  c# - What is the ...·  2 年前    · 
笑点低的毛豆  ·  Returning null from ...·  2 年前    · 
笑点低的毛豆  ·  asp.net core - Since ...·  2 年前    · 
笑点低的毛豆  ·  Task (JavaFX 8)·  2 年前    · 
笑点低的毛豆  ·  c# - Create a ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Why can't you use use the await keyword in the function that calls GetAllMapLocationItemsFromUserName? – Scott Chamberlain Aug 7, 2014 at 5:47 It breaks async because the caller does not wait for the function to finish. To force this, you can use await. – Serguei Fedorov Aug 7, 2014 at 5:50

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.

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.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.