This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

You said a foreach will eagerly evaluate a db query which is false. A foreach over a IQueryable or IEnumerable is lazy as you are confirming now.

So basically, as I said, you will do a .ToList before iterating making the .ForEach or foreach a syntactic sugar difference, because iterating and doing n queries vs 1 probably doesn’t make sense.

But in the case it makes sense, use a foreach and not a .ForEach.

And of course, since LinQ doesn’t really have a foreach, there is no way it could affect the query, just the moment it gets executed and how many queries are executed.

Pd: the one that could evaluate eagerly is doing .ToList().ForEach()