简体   繁体   中英

Convert type Task<IEnumerable> to type IEnumerable

I have the following expression which is of the type Task<IEnumerable<PendingApprovalUserChangeRequest>> and I need to convert it to IEnumerable<PendingApprovalUserChangeRequest> . How do I do that?

Task<IEnumerable<PendingApprovalUserChangeRequest>> pendingChangeRequest = service.GetPendingChangeRequest();

You can await it in an async method:

IEnumerable<PendingApprovalUserChangeRequest> result = await pendingChangeRequest;

You can read more about asynchronous programming here .

You should also read these best practices from MSDN if you have to introduce async in your codebase (which seems to be your case).

You have two paths:

  1. Use await before calling. In this case it will work asynchronously.

  2. Invoke .Result after calling. There your code will stop here and synchronously wait the result of the Task working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM