I had two people (including the person below who blocked me for my response) tell me using ToList() was wrong in a post I made on X.

Without additional context it’s impossible to say there’s anything wrong with using ToList/Async here or in general. The vast majority of EF doc code samples and snippets use ToList and it appears super regularly in blogs, tutorials, videos etc. and of course most importantly in working (and performant) production code too.
ToList / ToListAsync causes Entity Framework to send the query to the DB and materialize the results, it’s a super important part of the EF flow.
Are there occasions when using ToList is ‘wrong’ or better phrased … inappropriate? Of course
- ToList / ToListAsync puts all results for a query into memory. For large result sets where memory consumption could be an issue and your app is able to process one row at a time streaming with ForEach or AsEnumerable may be the better approach.
- We should also avoid using ToList (or ToArray) if we intend to use another LINQ operator on the result as this will needlessly buffer all results into memory prematurely, e.g.
var expensiveQuery = dbContext.Orders.ToList()
.Where(o => o.TotalAmount > 1000); // Filters in-memory
In the above case, calling ToList() returns all orders into memory with the filtering only done on the client side. Depending on the size of the Orders table of course this could have major performance implications.
So … know your context, it really does always depend.