Csharp/C#教程:System.Collections.Generic.List`1分享


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:Usersmyfile.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如上所示。

上述就是C#学习教程:System.Collections.Generic.List`1分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1027329.html

(0)
上一篇 2022年1月10日
下一篇 2022年1月10日

精彩推荐