System.Collections.Generic.List`1

我有以下类名为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 Children { get; set; } public Node() { Children = new List(); } 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(); 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系统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 Generic.List 1 [test.Node]]

替换Children.ToString(); 有:

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

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