Entity Framework Core: ToList() vs ToListAsync()

11,690

In terms of perf, ToListAsync probably has worse performance since async always brings an overhead.

That's not really why people use async though. Since your database call is fundamentally asynchronous, in the sync option your server's thread waits for the database call to finish before running the rest of the work and returning a result.

In the async approach, the thread can handle other requests while it waits for the database call to finish. So it offers better throughput. I'd go with the async approach all the way as it is pretty pointless to mark your controller action async and then do the biggest IO operation synchronously.

That said, the best option when thinking about perf is to measure. Make one action that is synchronous and another that is asynchronous. Then measure individual request response time and throughput. Then you can base your decision on actual numbers.

Share:
11,690

Related videos on Youtube

Dr.
Author by

Dr.

Updated on June 04, 2022

Comments

  • Dr.
    Dr. almost 2 years

    Please consider async GetAccountProperties method in ASP.NET Core Controller:

    public class CreditAccountController : Microsoft.AspNetCore.Mvc.Controller
    {
        private readonly ICreditAccountManager _creditAccountManager;
    
        public CreditAccountController(
            ICreditAccountManager creditAccountManager
        {
            _creditAccountManager = creditAccountManager;
        }
    
    
        [HttpGet]
        public async Task<IActionResult> GetAccountProperties(...)
        {
            var result = await _creditAccountManager.GetPropertiesAsync(...);
            if (accountResult.Code == ResponseCode.Success)
                return Ok(result.Value);
            else 
                return NotFound();
        }
    }
    

    and following implementations of GetPropertiesAsync() method in CreditAccountManager repository.

    First implementation uses .ToList() method:

    public Task<Result<List<GeneralAccount>>> GetAccountPropertiesAsync(...)
    {
        var properties = _generalDatabaseContext.AccountProperties.Where(...).ToList();
        result.Code = ResponseCode.Success;
        result.Value = accounts;
        return Task.FromResult(result);
    }
    

    The second one uses .ToListAsync() method and nested async/await:

    public async Task<Result<List<GeneralAccount>>> GetAccountPropertiesAsync(...)
    {
        var properties = await _generalDatabaseContext.AccountProperties.Where(...).ToListAsync();
        result.Code = ResponseCode.Success;
        result.Value = accounts;
        return result;
    }
    

    Considering, that there are no other EF calls in repository method and controller's method is already async, which implementation of repository method is better in terms of performance ?

    • Hasan Emrah Süngü
      Hasan Emrah Süngü over 5 years
      did you benchmark?
    • Kirk Larkin
      Kirk Larkin over 5 years
      better in terms of performance is a very broad term - ToListAsync() will free up the request-thread but might be slower overall, so it depends on what you're after.
    • pinkfloydx33
      pinkfloydx33 over 5 years
      Which repository implementation is better? Neither. EF already is a repository (and UoW)
  • Hans Kesting
    Hans Kesting over 5 years
    @DimitriRakviashvili - the operative term is "while it waits" - your first sample with Task.FromResult doesn't wait for anything. So the controller's "await" is blocked until the method returns without having the change to yield to other requests