CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Auto Incrementing value in Anonymous type collection returned by LINQ query

  1. #1
    Join Date
    Aug 2012
    Posts
    1

    Question 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!

  2. #2
    Join Date
    Apr 2010
    Posts
    131

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)