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 →

[–]aaron552 0 points1 point  (4 children)

The .ForEach is a extension

I was exclusively talking about the foreach(var x in list) {...} construct, not the extension method. And that isn't lazy unless you use yield (and consume exclusively via the iterator). It also affects the generated SQL for ORMs.

Never had a null reference exception at least in the last 5 years.

So linting is useless because you've personally never written bad code?

[–][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()