Search code examples
c#asp.netentity-framework

What method of EF 6 is better to use for get data from database async?


I have next code

public async Task<IEnumerable<MyTabel>> GetData()
        {
            try
            {
                var dbCtx = new myEntities();

                return await dbCtx.MyTabel.ToListAsync();
                //return await dbCtx.MyTabel.ToArrayAsync();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

I am wondering what is ToListAsync or ToArrayAsync method is better for perfomance ? does anyone know ?

Thanks.

UPDATE

for me performance is equal to less memory usage, faster query times, higher concurrency


Solution

  • ToList() is faster than ToArray(), because the array needs to be copied a second time once the size is known. (unlike List<T>, arrays cannot have extra space)
    The same is true for async versions.

    However, you may not want this function at all.
    Unless you really need to get all of the data on the client, it will be much more efficient to use LINQ to entities to run SQL queries in the database.