One Minute Knowledge: Is ToArrayAsync or ToListAsync faster for Entity Framework?

12/15/2025
1 minute read

Short question, and a short answer: ToListAsync - but why?

Let's check the code

ToListAsync is defined as:

public static async Task<List<TSource>> ToListAsync<TSource>(
    this IQueryable<TSource> source,
    CancellationToken cancellationToken = default)
{
    var list = new List<TSource>();
    await foreach (var element in source.AsAsyncEnumerable().WithCancellation(cancellationToken).ConfigureAwait(false))
    {
        list.Add(element);
    }

    return list;
}

Source: https://github.com/dotnet/efcore/blob/94b2ee91070174c06a1753b3121b8bda0db4f36a/src/EFCore/Extensions/EntityFrameworkQueryableExtensions.cs#L2302-L2313

And ToArrayAsync is defined as:

public static async Task<TSource[]> ToArrayAsync<TSource>(
    this IQueryable<TSource> source,
    CancellationToken cancellationToken = default)
    => (await source.ToListAsync(cancellationToken).ConfigureAwait(false)).ToArray();

Source: https://github.com/dotnet/efcore/blob/94b2ee91070174c06a1753b3121b8bda0db4f36a/src/EFCore/Extensions/EntityFrameworkQueryableExtensions.cs#L2337-L2338

So ToArrayAsync actually calls ToListAsync first, and then converts the resulting list to an array using ToArray(). So you get the overhead of creating a list, populating it, and then creating an array and copying the elements over.

This does not apply to ToHashSetAsync.

An error has occurred. This application may no longer respond until reloaded.Reload x