EF Core — IQueryable inside foreach

Sami C.
Oct 12, 2021

--

Did you know that the following code will work:

The 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.

Resolving query with .ToList

Make sure to read the following documentation about EF Core if you’re interested in performance!

Enjoy!

--

--

Sami C.
Sami C.

Written by Sami C.

Freelance Software Engineer

No responses yet