LINQ implementation - one loop or many?

Say I have something like collection.Select(..).Where(...).Sum(...)

Will the LINQ mechanism do 1 or more loops over the collection?

+5
source share
3 answers

For three teams there will be one cycle.

If you like more info, I recommend the Jon Skeet blog , where it describes the reimplementation of LINQ to Objects.

+2
source

Sum Where, Select, collection. Sum , , collection . .

, , :

class Sequence : IEnumerable<int> {
    public IEnumerator<int> GetEnumerator() {
        for (int i = 0; i < 17; i++) {
            Console.WriteLine(i);
            yield return i;
        }
    }

    IEnumerator IEnumerable.GetEnumerator() {
        return GetEnumerator();
    }
}

:

Sequence sequence = new Sequence();
int sum = sequence.Select(x => 2 * x).Where(x => x % 4 == 0).Sum();
Console.WriteLine("Sum is {0}", sum);

:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Sum is 144

, sequence .

+8

(- ), , .

, - Collection, .

"Collection" "X, X ".

"" " "

"" "".

. .

pokes . .

Select , Sum, pokes Where. .

. .

, . , . .

, . . , .

, Where. , , Where where that where accept.

: "" , , "", " ", .

When the collection ultimately says “no more” in “Where,” where “No more” for “Select,” “Select” means “no more” for the amount, and Sum returns you the amount.

+4
source

All Articles