Auto Incrementing value in Anonymous type collection returned by LINQ query
I have some unepected behavior and was hoping someone could shed some light on how the underlying pieces behind this code work to cause this issue.
The code below is intended to create a collection of an anonymous type in which during the creation an index is created for each object in the collection by incrementing the counter:
Code:
//Counter for key pair indexes
var i = 0;
//BuildKeyPairSet
var keyPairs = from m in queryString select new { key = m.Key, val = m.Value, index = i++};
The problem is what ends up happening at run-time is every single time one "looks into the collection" either by stepping through and using a quick watch in the IDE, or a for each loop etc., the value of the index property increments.
Any insight into why this is may not be aware of?
Thanks in advance!
Re: Auto Incrementing value in Anonymous type collection returned by LINQ query
Is i global in scope or contained within the same method as the linq query?