Is it correct if i am using await + ToListAsync() over IQueryable which is not defined as a task

后端 未结 3 2021
忘了有多久
忘了有多久 2020-12-10 13:01

I am using asp.net MVC-5 with EF-6, and I am not sure if using await + ToListAsync is valid. For example, I have the following repository method which returns a

3条回答
  •  长情又很酷
    2020-12-10 13:46

    At the beginning I thought I will get an error because I am using "await" a method which is not defined as a task, but the above worked well

    Actually, you are awaiting a method which returns a Task, where T is a List. If you look at the extension method QueryableExtensions.ToListAsync, you'll see that it returns a Task>. You are asynchronously waiting on this method to query the database, create the list and return it back to the caller. When you await on such a method, the method wont return until the operation has completed. async-await makes your code feel synchronous, while execution is actually asynchronous.

提交回复
热议问题