Foreach C # vs ForEach () EF6


Follow the code below:

% of EF6%:

var result = ctx.Table.Where(x => x.User == "João").ToList();
result.ForEach(x => x.Read = true);

ForEach() of C #:

var result = ctx.Table.Where(x => x.User == "João").ToList();
foreach (var item in result)
    item.Read = true;

I believe the above codes are the same functionality, what's the difference between them?

asked by anonymous 07.06.2017 / 20:45

2 answers


Contrary to what might seem, the Entity Framework's LINQ codes will attempt to generate an SQL expression or something equivalent that processes the data in the database. It will not execute exactly the code that is written there. This is called Domain Specific Language (DSL). This should occur with the Where() used before to get what is in the database.

But it should not occur with ForEach() that does not exist in the EF LINQ provider. So the difference between them is almost syntactic.

In order to know which delay you would have to do tests. The problem is that both are abstractions that have cost. foreach needs to get an iterator that is an object outside the collection to track the progress of each item. This is not free. Since ForEach() of List is a simple for without iterator, which is faster. On the other hand it needs to call a delegate on each iteration, which is not for free. If I give a test later, but it will take time because I'm extremely busy and the internet problematic.

In general, it is used for Parallel processing which is not possible with the command foreach , it is a little more expensive, but if there are gains (not always there) you can finish before running in parallel. In this case it is just a processing in the application on top of the result brought by the database.

There are people who like him in any situation because it gets shorter and eventually more semantic about what he wants to do. But the performance is not good if it does not parallel well. And parallelization is not available in every implementation of ForEach() . Contrary to what people imagine it is not a single method, it can have several flavors.

See more:

07.06.2017 / 21:06

The ForEach() method has no relation to the Entity Framework.

It is a method of class List , vide documentation . Note that it has no relation to LINQ, it is native to List .

There is no important difference between the two. They are two different ways of doing the same thing. In the background the method uses for to go through all the elements.

Here is the method code:

public void ForEach(Action<T> action) {
    if( action == null) {

    int version = _version;

    for(int i = 0 ; i < _size; i++) {
        if (version != _version && BinaryCompatibility.TargetsAtLeast_Desktop_V4_5) {

    if (version != _version && BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)

See more at

Obviously it changes a lot the way you write / read the code, which is better or more readable is a matter of taste.

I find it interesting to read this article from Eric Lippert that deals (not directly from your question, but try).

07.06.2017 / 20:53