I've been watching one course where the author mentioned that if you have some IQueryable
, say:
var someQuery = dbContext.Table.Where(x => x.Name == "Mark");
and then try to iterate over the results of someQuery
using foreach
, it keeps the database connection open until the whole foreach
ends, and a lot of beginner developers make this mistake and do tons of logic in the foreach
. Instead, she recommended just calling ToList()
up-front and then doing foreach
on the in-memory collection.
I can't find any reference on this though, that Entity Framework keeps the database open until the foreach
loop stops. How do I evaluate if this is true performance-wise?