EF Core — IQueryable inside foreach
Did you know that the following code will work:
IQueryable<Entity> will attempt to load the entire query result into memory when enumerated.
I prefer to use the
ToList()method to actually hit the database. In my opinion it’s a lot clearer than the example above.
Make sure to read the following documentation about EF Core if you’re interested in performance!
Efficient Querying - EF Core
Querying efficiently is a vast subject, that covers subjects as wide-ranging as indexes, related entity loading…