john

john

  • NA
  • 2
  • 556

Code is manipulating LINQ results somehow?

Nov 9 2020 2:39 PM
Hello fellow developers,
I have a small application which is doing something that I cannot explain.  
I use a "LINQ Where" query  to identify some nodes in a tree.  This returns an iterator of Tree Nodes that match the where criteria. I delete those nodes and when I check the contents of the LINQ iterator, it contains nodes which were not part of the LINQ query.  
 
Specifically the application does the following:
  • convert a list of folder paths to a simple tree structure
  • use LINQ to select terminating paths (folders with no sub-folders)
  • convert/copy the linq iterator to a List<Node> using "toList()"
  • loop over the list and delete these nodes from the tree
  • >> After processing, the LINQ iterator contains 2 nodes
The folder list resembles the following
         "\base",
         "\base\001",
         "\base\001\cfg",
         "\base\001\cfg\network",
         "\base\001\cfg\network\mgmnt", // terminating leaf
         "\base\001\cfg\network\users", // terminating leaf
         "\base\002",
         "\base\002\copy",
         "\base\002\copy\cfg",
         "\base\002\copy\cfg\network",
         "\base\002\copy\cfg\network\mgmnt", // terminating leaf
         "\base\002\copy\cfg\network\users", // terminating leaf
 
The LINQ iterator identifes 4 nodes (because these folders have no sub-folders)
         "\base\001\cfg\network\mgmnt"
         "\base\001\cfg\network\users"
         "\base\002\copy\cfg\network\mgmnt"
         "\base\002\copy\cfg\network\users"
 
If I inspect the LINQ iterator after deleting these 4 nodes from the tree, the iterator now contains 2 nodes
          "\base\002\copy\cfg\network"
          "\base\001\cfg\network" 
 
I would have expected the iterator to be empty.   
 
How does this happen???
 
Many Thanks,
Code Attached.

Answers (1)