'I can't filter elements in a collection with Enumerable.Where()
I found a strange behavior when I was using Linq.
The situation is that I want to make a collection which contains integers like [1, 2, 3, 4, 5], and I will remove the first element in the collection in each iteration of a for-loop.
But when I executed my code, I found that the count of elements in the collection didn't really reduce as what I expected. Instead, it kept remaining 4 elements after the first iteration.
Here is my source code:
var collection = Enumerable.Range(1, 5);
int currentNumber;
for (int i = 0; i < 10; i++)
{
currentNumber = collection.FirstOrDefault();
Console.WriteLine(currentNumber); //It prints endless 1 2 1 2 1 2...
collection = collection.Where(number => currentNumber != number);
}
I inspected the process with debugger and found the elements after executing collection.Where() were like:
Iteration 1: [2, 3, 4, 5]
Iteration 2: [1, 3, 4, 5]
Iteration 3: [2, 3, 4, 5]
Iteration 4: [1, 3, 4, 5]
...
This confused me because it seemed to always take [1, 2, 3, 4, 5] as the source to execute Where() in each iteration.
But when I moved the declaration of currentNumber into the for-loop, everything worked as what I expected.
var collection = Enumerable.Range(1, 5);
for (int i = 0; i < 10; i++)
{
int currentNumber; //Move the declaration to this line
currentNumber = collection.FirstOrDefault();
Console.WriteLine(currentNumber); //It correctly prints 1 2 3 4 5 0 0 0 0 0
collection = collection.Where(number => currentNumber != number);
}
Please tell me why it worked correctly after I moved the declaration into the for-loop.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
