Is it correct if i am using await + ToListAsync() over IQueryable which is not defined as a task
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<T>
, where T
is a List<TSet>
. If you look at the extension method QueryableExtensions.ToListAsync
, you'll see that it returns a Task<List<TSource>>
. 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 won't return until the operation has completed. async-await makes your code feel synchronous, while execution is actually asynchronous.
Actually there is no problem because you are awating the ToListAsync()
not the getAllScanEmailTo()
.
EDIT: To see how async-await pattern is working you can see this link. Here is a usefull image from there
You are not "awaiting a method". You are awaiting a Task
, which is an awaitable.
You call getAllScanEmailTo
that returns an IQueryable<TSet>
on which you then call ToListAsync
which returns the Task<List<TSet>>
you are awaiting.