3

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

2
  • 13
    Never write throw ex. Get rid of the catch block. Commented Aug 7, 2013 at 15:48
  • 1
    "better" is very subjective here. What defines "better performance"? Less memory usage? Faster query times? Higher concurrency? You need to be a lot more specific about what you're trying to optimise for. Commented Aug 7, 2013 at 15:52

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.