Click here to Skip to main content
15,884,629 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to find days between a defined date and now? Pin
Mycroft Holmes11-Nov-20 11:12
professionalMycroft Holmes11-Nov-20 11:12 
QuestionJSON "classes" repetitive Pin
Member 56973910-Nov-20 6:25
Member 56973910-Nov-20 6:25 
AnswerRe: JSON "classes" repetitive Pin
Gerry Schmitz10-Nov-20 7:24
mveGerry Schmitz10-Nov-20 7:24 
GeneralRe: JSON "classes" repetitive Pin
Member 56973910-Nov-20 8:06
Member 56973910-Nov-20 8:06 
GeneralRe: JSON "classes" repetitive Pin
Gerry Schmitz10-Nov-20 14:33
mveGerry Schmitz10-Nov-20 14:33 
QuestionColor the intersected rectangles in c# windows applicaion Pin
Member 147377589-Nov-20 19:47
Member 147377589-Nov-20 19:47 
AnswerRe: Color the intersected rectangles in c# windows applicaion Pin
OriginalGriff9-Nov-20 20:12
mveOriginalGriff9-Nov-20 20:12 
QuestionLINQ results getting modified? Pin
Stellar Developer9-Nov-20 13:52
Stellar Developer9-Nov-20 13:52 
I have run into an issue using "LINQ Where" on a tree structure, to create a list of leaf nodes. After I delete those nodes in a loop, I would expect "leaf nodes" to be an empty list. Instead it contains 2 nodes. Can anyone explain this behavior?

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 instead of 0 nodes

See 100 line example below...

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;

class Program
{
   static void Main(string[] args)
   {
      List<string> list = new List<string>
      {
         @"\base",
         @"\base\001",
         @"\base\001\cfg",
         @"\base\001\cfg\network", // <-- intruder
         @"\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", // <-- intruder
         @"\base\002\copy\cfg\network\mgmnt", // terminating leaf
         @"\base\002\copy\cfg\network\users", // terminating leaf
      };

      Tree tree = new Tree(list[0]);
      foreach (string item in list)
      {
         tree.Add(item);
      }

      var nodes_to_delete_iter = tree.Root.Descendants().Where(d => d.Children.Count == 0);
      var nodes_to_delete_list = nodes_to_delete_iter.ToList();

      int start_count = nodes_to_delete_iter.Count(); // <---- 4 Nodes selected here.   See "terminating leaf" in the folder list

      for (int index = nodes_to_delete_list.Count - 1; index > -1; index--)
      {
         Node leaf = nodes_to_delete_list[index];
         Console.WriteLine("Removing " + leaf.Path);
         nodes_to_delete_list.Remove(leaf);
         tree.Remove(leaf);
      }

      int final_count = nodes_to_delete_iter.Count(); // <---- At this point there are 2 nodes.  "See "intruder" in the folder list

      Console.ReadKey();
   }
}

public class Tree
{
   public Node Root { get; private set; }

   public Tree(string root_path)
   {
      Root = new Node(root_path);
   }

   public Node FindParent(string node_path)
   {
      int index = node_path.LastIndexOf("\\");
      string parent_path = node_path.Substring(0, index);
      Node parent_node = Root.Descendants().FirstOrDefault(n => n.Path == parent_path);
      return parent_node;
   }

   public Node Add(string node_path)
   {
      Node parent_node = FindParent(node_path);
      Node node = new Node(node_path);
      if (parent_node != null) { parent_node.Children.Add(node);  }
      return node;
   }

   public void Remove(Node node)
   {
      Node parent_node = FindParent(node.Path);
      parent_node.Children.Remove(node);
      node = null;
   }
}

public class Node
{
   public string Path { get; private set; }
   public List<Node> Children { get; private set; }

   public Node(string value)
   {
      Children = new List<Node>();
      Path = value;
   }

   public IEnumerable<Node> Descendants()
   {
      return new[] { this }.Concat(Children.SelectMany(child => child.Descendants()));
   }

   public override string ToString() // For Viewing in VS Debugger
   {
      return Path;
   }
}


modified 9-Nov-20 20:02pm.

AnswerRe: LINQ results getting modified? Pin
Gerry Schmitz9-Nov-20 14:49
mveGerry Schmitz9-Nov-20 14:49 
GeneralRe: LINQ results getting modified? Pin
Stellar Developer9-Nov-20 15:56
Stellar Developer9-Nov-20 15:56 
GeneralRe: LINQ results getting modified? Pin
Gerry Schmitz9-Nov-20 23:43
mveGerry Schmitz9-Nov-20 23:43 
SuggestionRe: LINQ results getting modified? Pin
Richard Deeming9-Nov-20 21:58
mveRichard Deeming9-Nov-20 21:58 
QuestionWhat's Wrong With This??? Pin
Kevin Marois8-Nov-20 19:00
professionalKevin Marois8-Nov-20 19:00 
AnswerRe: What's Wrong With This??? Pin
Jörgen Andersson8-Nov-20 20:11
professionalJörgen Andersson8-Nov-20 20:11 
AnswerRe: What's Wrong With This??? Pin
Richard Deeming8-Nov-20 21:47
mveRichard Deeming8-Nov-20 21:47 
GeneralRe: What's Wrong With This??? Pin
Kevin Marois9-Nov-20 7:42
professionalKevin Marois9-Nov-20 7:42 
QuestionC# - Use app between 2 times and get the time from database Pin
Valakik8-Nov-20 12:16
Valakik8-Nov-20 12:16 
AnswerRe: C# - Use app between 2 times and get the time from database Pin
Dave Kreskowiak8-Nov-20 18:54
mveDave Kreskowiak8-Nov-20 18:54 
AnswerRe: C# - Use app between 2 times and get the time from database Pin
Gerry Schmitz9-Nov-20 1:51
mveGerry Schmitz9-Nov-20 1:51 
QuestionWeird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
pr1mem0ver6-Nov-20 16:24
pr1mem0ver6-Nov-20 16:24 
AnswerRe: Weird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
Gerry Schmitz6-Nov-20 18:18
mveGerry Schmitz6-Nov-20 18:18 
GeneralRe: Weird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
pr1mem0ver7-Nov-20 5:00
pr1mem0ver7-Nov-20 5:00 
GeneralRe: Weird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
Gerry Schmitz7-Nov-20 5:37
mveGerry Schmitz7-Nov-20 5:37 
GeneralRe: Weird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
pr1mem0ver7-Nov-20 13:25
pr1mem0ver7-Nov-20 13:25 
GeneralRe: Weird problem with Custom StreamReader with Read override not reading correct number of bytes during base method call Pin
Gerry Schmitz8-Nov-20 5:53
mveGerry Schmitz8-Nov-20 5:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.