I once debugged this production error, and I desperately need help, and I'm also interested.
I simplified the logic of the code and added a printout for debugging:
int[] a = { 2,2,2 }; var b = a.Where(x => x==2); for(int i = 0; i < 3; i++) { var c = b.Where(x => x==i); Console.WriteLine("iter {0} before - B Count: {1}, C Count: {2}", i, b.Count(), c.Count()); if (c.Count() != b.Count()) b = b.Except(c); Console.WriteLine("iter {0} after - B Count: {1}, C Count: {2}", i, b.Count(), c.Count()); } Console.WriteLine("After Loop: B Count: {0}", b.Count());
Interesting (strange) output:
iter 0 before - B Count: 3, C Count: 0 iter 0 after - B Count: 1, C Count: 0 iter 1 before - B Count: 1, C Count: 0 iter 1 after - B Count: 1, C Count: 0 iter 2 before - B Count: 0, C Count: 0 iter 2 after - B Count: 0, C Count: 0 After Loop: B Count: 1
Question 1:
Why b.Count() == 0 in "iter 2 before". The only thing that happens between "iter 1 after and iter 2 before" is
var c = b.Where(x => x==i);
Why does this code even change?
Question 2:
Why b.Count() return to 1 after loop completion?
I am very grateful to everyone who helps me deal with this problem, thanks!