System.Collections.Generic.List`1 [test.Node]

时间:2014-04-14 20:28:05

标签: c#

我有以下名为Node

的类
 class Node
    {
        public int Id { get; set; }
        public int? ParentId { get; set; }
        public string Operator { get; set; }
        public Node Parent { get; set; }
        public IList<Node> Children { get; set; }
        public Node()
        {
            Children = new List<Node>();
        }
        public override string ToString()
        {
            return "Node: " + ParentId + " " + Operator + " " + Id + " " + Children.ToString();
        }
    }

我希望每个ParentId所有children

获得
  

[0节点:0~0 5,4]]       [1节点:12 + 1 2,3,5,]]       [2节点:12 + 2 7,5,3,]]       等等

 var map = new Dictionary<int, Node>();
 File.WriteAllLines(@"C:\Users\myfile.txt",
      map.Select(x => "[" + x.Key + " " + x.Value + "]").ToArray());

但我得到了

  

[0节点:0~0 System.Collections.Generic.List 1[test.Node]] [1 Node: 12 + 1 System.Collections.Generic.List 1 [test.Node]]       [2节点:12 + 2 System.Collections.Generic.List 1[test.Node]] [3 Node: 8 + 3 System.Collections.Generic.List 1 [test.Node]]       [4节点:8 + 4 System.Collections.Generic.List 1[test.Node]] [5 Node: 8 + 5 System.Collections.Generic.List 1 [test.Node]]       [6节点:8 + 6 System.Collections.Generic.List 1[test.Node]] [7 Node: 8 + 7 System.Collections.Generic.List 1 [test.Node]]       [8节点:2~8 System.Collections.Generic.List 1[test.Node]] [9 Node: 2 ~ 9 System.Collections.Generic.List 1 [test.Node]]

1 个答案:

答案 0 :(得分:1)

Children.ToString();替换为:

string.Join(",", Children.Select(x => x.Id));

您正在ToString上呼叫IList<Node>,因此它会为您提供类型名称。如果您想查看子节点的ID,可以使用string.Join,如图所示上方。